I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c is fine
29 is fine
2425315651252fsaw fails
24241jl.421c fails
c fails
The regex I have so far is (^\d+)([a-z]{1}|\d) which passes the 29, 20c, but also passes stuff like 29cdsd.
What am I doing wrong?
Your (^\d+)([a-z]{1}|\d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^ - start of string
[0-9]+ - any 1 or more digits
[a-z0-9]? - 1 or 0 lowercase ASCII letters or digits
$ - end of string.
This should follow your rules exactly.
^\d+[a-z]?$
if "any number of digits" might be zero ^\d*\w$
You could add an anchor $ to assert the end of the line and you can omit the {1} part:
^(\d+)([a-z]|\d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^\d+[a-z\d]$
Regex demo
That would match:
^ Assert the start of the string
\d+ Match 1+ digits
[a-z\d] A character class which matches a-z or a digit
$ Assert the end of the string
^ - start of string
\d* - any amount of digits, 0 or more
[a-zA-z] - a lowercase and uppercase ASCII letters.
$ - end of string
Related
Validate the string using the regex which has the - (hypen)
Requirement is : string contains only digits and - (hyphens) and not end with - (hyphen) and all other digits not be the same.
^([0-9-])(?!\1+$)[0-9-]{5}$
The above one allow only digits and hyphen but its not restricted end with hyphen and check all other digits are same.
ex:
1111-1 Not allowed because all are same digits
1111-2 Allowed
11112- Not allowed as its end with - Hypen
-12345 Not allowed as its start with - hypen
You might write the pattern as
^(\d)(?!(?:\1|-)+$)(?!\d*-\d*-)[\d-]{4}\d$
Explanation
^ Start of string
(\d) Capture a single digit in group 1
(?! Negative lookahead
(?:\1|-)+$ Check that to the right there is not only the group 1 value or hyphens
(?!\d*-\d*-) Assert not 2 hyphens
) Close lookahead
[\d-]{4} Match 4 digits or hyphens
\d Match a digit
$ End of string
Regex demo
If there should be at least 1 hyphen:
^(\d)(?!(?:\1|-)+$)(?=\d*-)[\d-]{4}\d$
Regex demo
My 2 cents to allow [01] hyphens:
^(?=.{6}$)(\d)(?=.*(?!\1)\d)\d+(?:-\d+)?$
See an online demo
I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.
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
I am writing an expression to validate a username with the following requirements:
length between 6 and 20,
must start with a letter,
numbers are allowed,
dot is optional or only one is allowed.
must not end with a dot
Tried with but didn't work:
^(([a-zA-Z])(\.{0,1})([a-zA-Z0-9]*)){6,20}$
another option
^([a-zA-Z]+([\\.]?)+([a-zA-Z0-9]*)){6,20}$
You may use an expression that will match a letter at the start and the 5 to 19 letters, digits or dots that should only appear in the middle of the string.
You may use
^(?=.{5,19}$)(?=[A-Za-z])[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*$
See the regex demo.
Details
^ - start of string
(?=.{5,19}$) - a positive lookahead that matches a string that contains any five to nineteen characters other than line break chars
(?=[A-Za-z]) - the first char must be an ASCII letter
[A-Za-z0-9]+ - one or more letters or digits
(?:\.[A-Za-z0-9]+)* - 0 or more repetitions of
\. - a dot
[A-Za-z0-9]+ - one or more letters or digits
$ - end of string.
I would like to match String that contains letters and numbers WITHOUT space
I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters
This is what it should do :
Nope
Nope 2
MatchPlease123
If you want to try in live:
http://rubular.com/r/pFMkk9ATc0
Thank you
You may use
/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/
See the regex demo (a bit modified since the input is a multiline string).
Details:
^ - start of a string
(?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
(?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
[a-zA-Z0-9]* - 0+ ASCII letters or digits
$ - end of string.
I think this simplest one will also be helpful
Regex demo
Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$
1. ^ start of string.
2. (?=.*\d) positive look ahead for digit.
3. (?=.*[a-zA-Z]) positive look alphabets.
4. [a-zA-Z\d]+ match all digits A-Z and a-z
5. $ end of string.