Regular expression for 1-31 [duplicate] - regex

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

Related

Regular expression: Should not start with 5 digits (or more) in a row [duplicate]

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

Regex to find specified Value [duplicate]

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.

If Else regex matching [duplicate]

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})

Regex for string between a digit and character? [duplicate]

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

How to do regexp replace/search [duplicate]

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.