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.
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 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:
Regular Expression to match a valid day in a date
(11 answers)
Closed 4 years ago.
I'd like to make a reglar expression which checks date digit e.g. 1 through 31.
I think it has three patterns : [0-9], [12][0-9], 3[01].
But I don't know how to make them as one regex with |(or) operator.
Anybody has idea for this?
What you need is alternation and a group to make the pattern local. Just use a | within the capture group (a|b) or non-capturing group (?:a|b):
([1-9]|[12][0-9]|3[01])
Updated:
([0-2][0-9]|(3)[0-1])
Hope this answer your question
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
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I am following some instructions for data upload. I can't figure out what the following two points mean. Does anyone have any idea?
Regexp search/replace
search: 201([0-9])([0-9])([0-9])([0-9][0-9]) ([0-9])
replace:201\1\2\3\4 \5
Regexp search/replace
replace 20110401 with whatever year month day that is being fixed
^(.{462})
\120110401
Any decent regex tutorial will help.
() wrap groups that can be referenced later with \#. For example, \2 references the token matched by the second pair of parentheses.
[0-9] means any character between 0-9 inclusive.
^ is the left anchor (i.e., start of string or new line), and .{462} means any character, 462 times.