Regex match exactly 1 anywhere in string - regex

So I need to match upper and lower case a-z letters, period (.) and # in a string. As a complication the string must have # exactly once anywhere in the string and . at least once anywhere in the string.
abcd#. // match
#ab.cd // match
a#cd#. // no match
abcd# // no match
I've tried to be clever (obviously not very) by doing look ahead but this one seems tricky eg.
(?=[#]){1}[a-zA-Z#]+$

The (?=[#]){1}[a-zA-Z#]+$ pattern matches any substring that starts with # and then has zero or more letters or # up to the end of the string. Look at what it matches.
You need to use
^(?=[^#]*#[^#]*$)(?=[^.]*\.)[a-zA-Z#.]+$
Or, if there must be also one dot (and no more than one) in the string
^(?=[^#]*#[^#]*$)(?=[^.]*\.[^.]*$)[a-zA-Z#.]+$
See the regex demo #1 and the regex demo #2.
Details
^ - start of string
(?=[^#]*#[^#]*$) - requires only one # and no more than one in string - a positive lookahead that requires 0+ chars other than #, a #, and again zero or more chars other than # till the end of string
(?=[^.]*\.) - requires at least one dot - a positive lookahead that requires 0+ chars other than . and then a .
(?=[^.]*\.[^.]*$) - requires only one dot and no more than one in string - a positive lookahead that requires 0+ chars other than ., a ., and again zero or more chars other than . till the end of string
[a-zA-Z#.]+ - one or more ASCII letters, # or .
$ - end of string.

Another option could be using a single lookahead asserting # and match a dot between 2 character classes, or the other way around asserting a dot and matching #
^(?=[^#]*#[^#]*$)[A-Za-z#]*\.[A-Za-z#]*$
Explanation
^ Start of string
(?=[^#]*#[^#]*$) Assert only 1 # char in the string
[A-Za-z#]*\.[A-Za-z#]* Match a dot between optionally repeating character classes each matching 1 out of A-Za-z#
$ End of string
Regex demo
For and . at least once anywhere in the string , you can allow matching a dot in the second character class:
^(?=[^#]*#[^#]*$)[A-Za-z#]*\.[A-Za-z#.]*$
Regex demo

I'm thinking you could just use:
^(?=.*\.)[a-zA-Z.]*#[a-zA-Z.]*$
See the online demo.
^ - Start string ancor.
(?=.*.) - Positive lookahead for any amount of characters up to a literal dot.
[a-zA-Z.]* - Zero or more characters from upper/lowercase letters or a dot.
# - A single #.
[a-zA-Z.]* - Zero or more characters from upper/lowercase letters or a dot.
$ - End string ancor.

Related

Regex match string 3-6 characters long, at least one letter, no duplicate "-"

I have to match a string that is 3-6 characters long, contains at least one letter, but can have letters, numbers and only 1 "-".
The "-" must not be at the start or at the beginning.
Match:
string
str-ng
st-ng
s1-1g
st-1g
Do not match:
strings
-string
string-
st--ng
s-tn-g
1111
st
The closest I've gotten is this:
^((?!-.*-)[0-9A-Z]{3,6})$
But this divides the regex match with - So it matches s-tri but not st-ri because there aren't 3 chars at each end
Maybe you can use:
^(?=.*[a-z])(?!-|.*-$|.*-.*-)[a-z\d-]{3,6}$
See the online demo
^ - Start string anchor.
(?=.*[a-z]) - Positive lookahead to make sure there is at least one letter.
(?!-|.*-$|.*-.*-) - Negative lookahead to prevent a hyphen at the beginning or at the end or multiple.
[a-z\d-]{3,6} - Three to six times a character from the give class.
$ - End string anchor.
Note that I used the case-insensitive flag.
You can use
^(?=.{3,6}$)(?=[^a-zA-Z]*[A-Za-z])[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?$
See the regex demo. Details:
^ - start of string
(?=.{3,6}$) - string must contain three to six chars other than line break chars
(?=[^a-zA-Z]*[A-Za-z]) - there must be at least one ASCII letter in the string
[0-9a-zA-Z]+ - one or more alphanumeric ASCII chars
(?:-[0-9a-zA-Z]+)? - an optional sequence of - and then one or more alphanumeric ASCII chars
$ - end of string.
Looking at the pattern that you tried, you meant to exclude the match when there are 2 hyphens present using the negative lookahead.
Also this part [0-9A-Z]{3,6} does not match a hyphen.
Reading
The "-" must not be at the start or at the beginning.
You might do that using
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{2,5}$
Regex demo
If you meant also no - at the end:
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{1,4}[a-zA-Z0-9]$
Explanation
^ Start of string
(?![^\n-]*-[^\n-]*-) Assert not 2 times -
(?=[^a-zA-Z\n]*[a-zA-Z]) Assert a char a-zA-Z
[a-zA-Z0-9] Match One of the listed without -
[a-zA-Z0-9-]{1,4} Repeat 1-4 times any of the listed including -
[a-zA-Z0-9] Match One of the listed without -
$ End of string
Regex demo

Regex which matches all alpha numeric chars and zero or one '#' symbol

I need a regex which matches all alpha numeric chars and zero or one '#' symbol in any part of the string, so:-
Ab01# - match
Ab0#1 - match
#Ab01 - match
here's what I have:-
/^[A-Za-z0-9]+#{0,1}$/
The above matches the '#' when it's at the end of the string but doesn't match when it's at the start or in the middle, for example
#Ab01 - no match
Ab#01 - no match
I've tried removing the ^ & $ indicating start and end of the expression - but this allows more than one match of the # which is not what I want.
If the # can be there only a single time, you can match optional chars from [A-Za-z0-9] and optionally match an # in between.
If you don't want to match empty strings and a negative lookahead is supported:
^(?!$)[A-Za-z0-9]*#?[A-Za-z0-9]*$
Regex demo
If there has to be at least a single char of [A-Za-z0-9] present, you could also use
^(?=#?[A-Za-z0-9])[A-Za-z0-9]*#?[A-Za-z0-9]*$
Regex demo
Alternatively maybe use:
^(?!.*#.*#)[A-Za-z\d#]+$
See the demo.
^ - Start string ancor.
(?!.*#.*#) - Negative lookahead to prevent multiple "#".
[A-Za-z\d#]+ - One or more characters from the specified character class.
$ - End string ancor.

Greedy regex quantifier not matching password criteria

/(^[a-zA-Z]+-?[a-zA-Z0-9]+){5,15}$/g
regex criteria
match length must be between 6 and 16 characters inclusive
must start with a letter only
must contain letters, numbers and one optional hyphen
must not end with a hyphen
the above regular expression doesnt satisfy all 4 conditions. tried moving the ^ before the group and omitting the + quantifiers but doesnt work
You are setting the limiting quantifier on a group that already has quantified subpatterns, thus, the length restriction won't work.
To set the length restriction, add the (?=.{6,16}$) lookahead after ^ and then feel free to set your consuming pattern.
You may use
/^(?=.{6,16}$)[a-zA-Z][a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)?$/
See the regex demo. Note you should not use g modifier when validating the whole input string against a regex.
Details
^ - start of string
(?=.{6,16}$) - 6 to 16 chars in the string input allowed/required
[a-zA-Z] - a letter as the first char
[a-zA-Z0-9]* - 0+ alphanumeric chars
(?:-[a-zA-Z0-9]+)? - an optional sequence of - and then 1+ alphanumeric chars
$ - end of string.
All you need
^(?i)(?=.{6,16}$)(?!.*-.*-)[a-z][a-z\d-]*\d[a-z\d-]*(?<!-)$
Readable
^
(?i)
(?= .{6,16} $ ) # 6 - 16 chars
(?! .* - .* - ) # Not 2 dashes
[a-z] # Start letter
[a-z\d-]* # Optional letters, digits, dashes
\d # Must be digit
[a-z\d-]* # Optional letters, digits, dashes
(?<! - ) # Not end in dash
$
Well, at least my regex forces a number be present.

How Can i write a regexp that will allow only digits and comas and only digits at the beginning and the end of the string?

How can i write a regexp, that will check if string starts and ends with digits and in between contains only digits and comas? Comas must also be separated from each other with at least one digit. For the conditions above i have following regexp: ^\d(,?\d)*$ but i have following additional condition: All comma separated integers, that are composed by sequences of digits, must be different from each other. What would be the regexp that allows only this kind of strings?
Thank you
First of all, your regex contains unquantified \d, and that matches only single digits. You need to add + after \d to match 1 or more digits.
To avoid having duplicate values, you may use
^(?!.*\b(\d+)\b.*\b\1\b)\d+(?:,\d+)*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
The (?!.*\b(\d+)\b.*\b\1\b) is a negative lookahead that will fail the match if after any 0+ chars other than line break chars, there is a group of digits that appear later in the string (after another 0+ chars other than line break chars) again.
Details
^ - start of string
(?!.*\b(\d+)\b.*\b\1\b) - a negative lookahead that fails the match if identical values appear in the text
\d+ - 1+ digits
(?:,\d+)* - zero or more occurrences of
, - a comma
\d+ - 1+ digits
$ - end of string.

Regex string doesn't contain 2 dots in a row

I'd like to know if this regex expression is correct for checking that a string doesn't start with a dot, doesn't end with a dot and contains at least one dot anywhere but not the start or end:
My issue is that I can't figure on how to check if there's 2 dots in a row.
/^([^.])+([.])+.*([^.])$/
It seems you need to use
^[^.]+(?:\.[^.]+)+$
See the regex demo
Details:
^ - start of string
[^.]+ - 1+ chars other than a . (so, the first char cannot be .)
(?:\.[^.]+)+ - 1 or more (thus, the dot inside a string is obligatory to appear at least once) sequences of:
\. - a dot
[^.]+ - 1+ chars other than . (the + quantifier makes a char other than . appear at least once after a dot, thus, making it impossible to match the string with 2 dots on end)
$ - end of string.
You're close, have a try with:
^[^.]+(?:\.[^.]+){2,}$
It maches strings that have 2 or more dot, but not at the begining or at the end.
If you want one or more dot:
^[^.]+(?:\.[^.]+)+$
If you want one or two dots:
^[^.]+(?:\.[^.]+){1,2}$