I need a regex that matches at most 9 digits with any number of space and/or hyphen (leading, trailing or within the digits), what should it look like?
I tried:
^[0-9 \\-].*?$
and
^\\d{9}
but they only serve part of my purpose and need a way to merge them together.
Thanks!
Try this regex:
^(?:[ -]*\d[ -]*){1,9}$
Click for Demo
Explanation:
^ - asserts the start of the string
(?:[ -]*\d[ -]*){1,9}
[ -]* - matches 0+ occurrences of either a space or a -
\d - matches a digit
[ -]* - matches 0+ occurrences of either a space or a -
{1,9} - matches 1 to 9 occurrences of a digit preceded or succeeded by either 0+ spaces or 0+ -
$ - asserts the end of the string
Related
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
How can I allow one subsequent dash character in the body part but not at start or end?
https://regex101.com/r/D8MAXP/8/
One subsequent example: https://regex101.com/r/D8MAXP/9/
Regex
^((https?):\/\/)?(www.)?([a-z0-9-])+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$
Allow:
http://www.b-c.de
https://www.b-c.de
www.b-c.de
b-c.de
Don't allow:
https://www.foufos-.gr
http://www.foufos-.gr
https://-foufos.gr
http://foufos-.gr
www.-foufos.gr
www.foufos-.gr
www.-foufos.gr
foufos-.gr
-foufos.gr
Instead of matching the - in the character class, you could take it out and use a repeating group prepending the hyphen before the character class
Use a * to repeat it 0+ times or a ? to match it zero or 1 times.
For the example data in the question, you might use
^((https?):\/\/)?(www\.)?[a-z0-9]+(?:-[a-zA-Z]+)*(?:\.[a-z]+)+(\/[a-zA-Z0-9#]+\/?)*$
Regex demo
For all the links in the regex101 example, you might use for example 2 negative lookaheads:
^(?!ww?\.)(?:https?:\/\/)?(?:www\.)?(?!.*\.www\b)[a-z0-9]+(?:-[a-zA-Z]+)*(?:\.[a-z]+)+(?:\/[a-zA-Z0-9#]+\/?)*$
In parts
^ Start of string
(?!ww?\.) Assert not starting with 1 or 2 times a w char followed by a .
(?:https?:\/\/)? Optionally match the protocol part
(?:www\.)? Optionally match www.
(?!.*\.www\b) Assert that what is on the right is not again www.
[a-z0-9]+(?:-[a-zA-Z]+)* Match chars a-z0-9 optionally repeated by a - and again chars a-z0-9
(?:\.[a-z]+)+ Repeat 1+ times a dot and 1+ chars a-z
(?:\/[a-zA-Z0-9#]+\/?)* Repeat 0+ times matching / and 1+ times any of the listed followed by an optional question mark
$ End of string
Regex demo
/(^[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.
I was using the below pattern.
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/.
What i need is that it should not be allowing the hyphen between 2 numbers.
I know that we have to make modification with 0-9, where we can restrict user from entering them twice.
The (?!.*[0-9]-[0-9]) lookahead after ^ will make sure there is no digit-digit pattern in the string. Also, if there must be 1 or 0 hyphens, replace * at the end with ? (0 or more occurrences).
Use
^(?!.*[0-9]-[0-9])[A-Za-z0-9]+(-[A-Za-z0-9]+)?$
See the regex demo.
Details
^ - start of string
(?!.*[0-9]-[0-9]) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, there is a digit, hyphen, digit pattern
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
(-[A-Za-z0-9]+)? - 1 or 0 sequences of:
- - a hyphen
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
$ - end of string.
I need to check if the string contains from 0 to 3 spaces and 16 digits. How can I do this ? All that I come up with is only for checking the sum
^[0-9- ]{16,19}$
You actually should use
^(?=(?:[^ ]* ){0,3}[^ ]*$)(?=(?:[^0-9]*[0-9]){16}[^0-9]*$)[0-9- ]+$
See the regex demo at regex101.com.
Alternatively, the first space checking positive lookahead may be replaced with a negative one with reverse logic:
^(?!(?:[^ ]* ){4})(?=(?:[^0-9]*[0-9]){16}[^0-9]*$)[0-9- ]+$
See another demo
Both the regexps are written with the principle of contrast in mind, so as to fail the regex quicker if the lookahead pattern does not match.
Details:
^ - start of string
(?!(?:[^ ]* ){4}) - a negative lookahead failing the match if there are 4 sequences immediately to the right of the current location, of:
[^ ]* - 0+ chars other than a space
- a space
(?=(?:[^0-9]*[0-9]){16}[^0-9]*$) - a positive lookahead requiring that the whole string should contain 16 sequences of 0+ non-digits ([^0-9]*) followed with 1 digit, and then 0+ chars other than a digit up to the end of the string
[0-9- ]+ - matches 1+ chars that are either digits, - or spaces
$ - end of string.
You can use this regex based on lookaheads:
^[0-9](?!(?:[0-9]* ){4})(?=(?: *[0-9]){15}$)[0-9- ]+[0-9]$
RegEx Demo
^[0-9] and [0-9]$ ensures we have only digits at start and end.
(?!(?:[0-9]* ){4}) is negative lookahead to disallow 4 spaces (thus allowing 0 to 3 whitespaces)
(?=(?: *[0-9]){16} *$) is positive lookahead to allow exactly 16 digits in the input surrounded by optional spaces.