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'
Related
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.
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 answers here:
Match exact string
(3 answers)
Closed 4 years ago.
Hi this is my example 0xc1e5017fbc68aa3e56aa580708de9aa123d62d18
This is my reg ex ^0x[a-fA-F0-9]{1,40}. What I'm doing is 0x which is compulsory after that we can enter upto 40 alpha numeric digits. But not special characters. What should I do here so it will match correctly?
Your issue is that you need to assert where the string ends, or else you will match up until a special character is found, and get undesired matches.
So with your current regex and test string 0xc1!, 0xc1 would be matched, even though it is an invalid match.
How about using: ^0x[a-fA-F0-9]{1,40}$
This question already has answers here:
Regular expression to get last 3 characters of a string
(2 answers)
Closed 5 years ago.
how to get last 3 or 5 digits from this given one using regex
4c113866bc43e2be48bbf85be829d7bc
Use $ anchor to mark the end of the string:
.{3,5}$
3 above is the min number of characters to match; 5 is max.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to have a regular expression to restrict the number of digits in a string which may contain alphabets or any other characters along with digits. Is this possible with regular expressions ?
The idea is to anchor beginning and end of string and allow exactly n repetitions of a digit with something else. For example, exactly 6 digits in a string:
^\D*(\d\D*){6}$