This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I have a few different strings that look like this:
aaaa01b.site.com
bbbb01ccc.site.com
cccc02dd.site.com
dd03eeee.site.com
All I am interested in is the characters between the last numeric digit and the first full stop, ie
b
ccc
dd
eeee
Is there a regular expression that can achieve this?
Try this pattern:
.*\d+([^.]+)\.
The characters you want should be available in the first capture group.
Demo
Related
This question already has an answer here:
Regular expression for a string that does not start with a sequence
(1 answer)
Closed 1 year ago.
I need to generate a regular expression to validate that the string does not start with 5 digits.
NOT VALID: 12345testing123asd
VALID: 1234testing1234
testing12345
testing
I tried to get the first five chars ^.{0,5} but I do not know hot to add the restriction of \D to those first 5 chars
Also, I tried with this [^0-9][^0-9][^0-9][^0-9][^0-9] but I do not know how to do to include the strings that starts with 4 or less numbers
Could you please help me with this? I am a rookie :(
If your RegExp flavor of choice supports negative lookaheads, this pattern will match if the string is valid (does not start with 5 or more consecutive digits):
^(?!\d{5,})
Regex101
Matches:
1234testing1234
testing12345
testing
Does not match:
12345testing123asd
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:
PHP regex groups captures
(4 answers)
Closed 3 years ago.
I need a regex to get only the month of value between string and year, and one regex to find only the year. Any number of spaces can exist.
What I tried:
(?<=MyString)([\s]*)((?:Jan(?:uar)?|Feb(?:ruar)?|Mär(?:z)?|Apr(?:il)?|Mai?|Jun(?:i)?|Jul(?:i)?|Aug(?:ust)?|Sep(?:tember)?|Okt(?:ober)?|Nov(?:ember)?|Dez(?:ember)?)) ((?:19[7-9]\d|2\d{3})(?=\D|$))
I cannot figure out how to ignore the spaces. How can I get this result?
Sample:
MyString Januar 2019
Regex 1: Januar
Regex 2: 2016
The regex just needs a whitespace construct \s* between the month and year :
(?<=MyString)(\s*)((?:Jan(?:uar)?|Feb(?:ruar)?|Mär(?:z)?|Apr(?:il)?|Mai?|Jun(?:i)?|Jul(?:i)?|Aug(?:ust)?|Sep(?:tember)?|Okt(?:ober)?|Nov(?:ember)?|Dez(?:ember)?))\s*((?:19[7-9]\d|2\d{3})(?=\D|$))
Note that there should be no need to capture the whitespace (\s*)
at the beginning unless it is being used as a flag in a code sense.
This question already has answers here:
regular expressions: match x times OR y times
(4 answers)
Closed 3 years ago.
I want to build a regex where it searches for a string containing 12 digits in a row. If there's no match, look for a string with only 10 digits in a row.
For example:
a123456789012a
a1234567890a
Would return:
123456789012
And if the input is:
a1234a
a1234567890a
It would return:
1234567890
I managed to create the regex for the individual operations, beeing (?<!\d)\d{10}(?!\d) for 10 digits and (?<!\d)\d{12}(?!\d) for 12 digits, but I can't group them up in a if-else style.
I tried the following:
(?(?<!\d)\d{12}(?!\d)|((?<!\d)\d{10}(?!\d)))
but if the first pattern don't match, the regex don't try to match the second, returning nothing
You can use a simple regex like this:
\d{12}|\d{10}
working demo
Look that I have not used multiline nor global flags. This way the pattern is going to find the first match you want.
Case 1:
Case 2:
BTW, use capturing groups if you want to capture the content:
(\d{12}|\d{10})
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'