This question already has answers here:
How to match repeated patterns?
(4 answers)
Closed 5 months ago.
I would like to match a string with the following structure string1-string2-string3 where each string can include numerics but no capital letters.
a concrete example would be "hello-world-today"
I have a regex expression, but I am sure it's not optimal since I repeat the same pattern three times.
'([a-z0-9]([-a-z0-9]*[a-z0-9])?)-(([a-z0-9]([-a-z0-9]*[a-z0-9])?))-(([a-z0-9]([-a-z0-9]*[a-z0-9])?))'
This should be an effective way:
^(?:[a-z\d]+-){2}([a-z\d]+)$
It matches two groups of lowercase letters and numbers followed by a dash and then another group of the same characters without the dash at the end.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a string in the following format:
ABC12233434343DEF
How can I extract only:
ABC12233434343
I want to leave out the ending set of characters of whatever length they might be.
There are several ways this is one:
.*?\d+
It will match anything at the beginning that is followed by numbers.
It may be also posible to limit the characters it can match initially, like if you was capital letters from A-Z, for example:
[A-Z]+\d+
Online Demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to write a regular expression for a password with these conditions:
1- The password must contains 4 letters at least.
2- The password must contains 2 digits.
3- The password must contains 2 punctuation symbols.
4- The password must have a minimum length of 8 characters.
This is the regular expresion that I have: "^((?=.*?\\p{L}.{4,})(?=.*?\\p{N}.{2,})(?=.*?\\p{P}.{2,})).{8,}$"
And my problem is that yhis expresion doesn't accept this as password an I don't know why: "abcd12.-".
Can anyone help me with this problem?
Thank you
This part in the expressions (?=.*?\\p{L}.{4,} checks if there is a letter followed by 4 or more times any character.
You could update your expression to use non capturing groups (?: and repeat the groups n times matching a letter, a number or punctuation instead.
^(?=(?:.*\\p{L}){4})(?=(?:.*\\p{N}){2})(?=(?:.*\\p{P}){2}).{8,}$
Regex demo
Note that using .{8,} matches at least 8 times any character except a newline, also matching spaces.
This question already has answers here:
PHP: RegEx for string with 3-9 letters and 5-50 numbers
(2 answers)
Closed 5 years ago.
I want to match a string that has the following requirements:
The string contains 5 to 10 digits. [0-9]
Contains 2 to 3 letters. [a-z]
for example
123a2bc12
s123d456
This is my regular expression. /^\d*[a-z]\d*[a-z]\d*[a-z]?\d*$/. But this can only limit the number of letters.
It isn't pretty but this should be what you're looking for:
^(?=(?:[^a-zA-Z]*[a-zA-Z]){2,3}[0-9]*$)(?=(?:[^0-9]*[0-9]){5,10}[A-Za-z]*$).*$
I think you want to this
s=re.search(r'(\w+)','123a2bc12').group(1)
print s #The output is '123a2bc12'
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
Does this regular expression mean that at least one of the following that isn't a-z:
(?=.*(?:[a-z]))
It's part of the following expression:
/^(?=[A-Za-z0-9\'\s\d\.]{2,50}$)(?=.*(?:[a-z]))[a-zA-Z0-9]+[A-Za-z0-9\'\s\.]+$/m
No, (?=.*(?:[a-z])) means that there could be whatever but must finish with a lowercase letter.
This regex means:
/^(?=[A-Za-z0-9\'\s\d\.]{2,50}$)(?=.*(?:[a-z]))[a-zA-Z0-9]+[A-Za-z0-9\'\s\.]+$/m
Match the line that starts with 2 to 50 alphanumeric, single quote, spaces or a dot, and then follows with lower case letter, and continues with alphanumerics and must ends followed by alphanumerics, spaces, single quote or dot.
Here you can see a better graphical approach for your regex:
Actually, this can be improved as:
/^(?=[A-Za-z\d'\s.]{2,50}$)(?=.*[a-z])[a-zA-Z\d]+[A-Za-z\d'\s.]+$/m
This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.