Day, Month text input: intelligent regex on the month part? - regex

I have a date entry control on a UI. It auto-completes the date as numbers are typed.
Currently using:
\d{1,2}(\.|\/|-)\d{1,2}
Test on http://regexpal.com/ to see it working.
Basically 1 or 2 digits, followed by one of ./-, and 1 or 2 digits. e.g. 01/9, 1/2, 1/1, will match
What I'm looking to do (and can't!):
If the second set of digits (the Month part) is 0 or 1, need another digit
If the second set of digits is 2-9, match
That's it basically, thanks
Brian

Number ranges are a bit annoying in regex, and should maybe be done in a separate step, when the format is validated. But here is the regex:
\d{1,2}[/.-](0?[2-9]|1[0-2]|01|1[/.-])
That should match all the requirements (including the ones from the comments). Possible months:
2, 3, ..., 9
01, 02, 03, ..., 09
10, 11, 12
1/, 1., 1-

Related

Regex for month with optional leading 0

I am trying to match various months, that may be in the form of:
01
1
12
13
09
All of the above inputs are valid except for 13.
The current regex I have for this is:
0?(?#optional leading 0, for example 04)
\d(?#followed by any number, 01, 2, 09, etc.)
|(?#or 10,11,12)
1[012]
What's wrong with the above regex? Here's an example link: https://regex101.com/r/cujCmD/1
I would phrase the regex as:
^(?:0?[1-9]|1[012])$
Demo
The parentheses and anchors are needed to ensure that the alternation chosen gets applied to the entire number input.

Regular expression pattern for the date format 1st mon/month yyyy and mon/month, 1, yyyy

I have regex pattern for the following date formats but they are not working as expected
1st mon/month yyyy(which should match date of type 2nd April 1785,1st April 1999,3rd April 2000,4th Jan 1999)
mon/month, 1, yyyy(which should match date of type November 2, 2000,Nov 4, 2000)
1st mon/month yyyy - \s?(?:0?[1-9]|[1,2][0-9]|3[0-1])(?:nd|rd|th|st)?(?:[\s|,|]?\s?)(?:[Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]une|[Jj]uly|[Aa]ug(?:ust)?|[Ss]ept(?:ember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)\s\d{4}
mon/month, 1, yyyy - \s?(?:[Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]une|[Jj]uly|[Aa]ug(?:ust)?|[Ss]ept(?:ember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)(?:[\s|,]?\s?)(?:0?[1-9]|[1,2][0-9]|3[0-1])(?:[\s|,]?\s?)\s\d{4}
Both the formats are taking more than 4 digits in a year say I can give November 2, 200000000 and it is matching successful it should accept only 4 digits
Try adding \b to the ends of both your regexes. This forces the date to break at a "word boundary", meaning if you have more than four digits, the regex won't match.
ab\d{4} will match ab1234 and will "match" ab123456, up to the 4 - I'm assuming you want this second case to fail because it's badly formatted, and has too many numbers.
ab\d{4}\b will NOT match ab123456, but will match ab1234.

Regex all even numbers, highest number 12

I'm trying to get a regex to get only even numbers and the highest number that can be entered should be 12.
I got this to work for only even number:
^(\d*[02468])$
Now I just need help with getting the highest number that can be entered is 12. I tried:
^(\d*[02468]|[0-1][0-2])$
but that didn't work. Any suggestions?
Your ^(\d*[02468]|[0-1][0-2])$ regex matches (with \d*[02468]) 0 or more digits followed with 0, 2, 4, 6 (so, it can match 32) or (with [0-1][0-2]) 8 or 00, 01, 02, 10, 11 or 12. As you see, they are not all even.
You may use
^([02468]|1[02])$
Or with a leading optional 0:
^(0?[02468]|1[02])$
See the regex demo
Details:
^ - start of string
( - Grouping construct matching either
[02468] - a digit from the set
| 0- or
1[02] - 10 or 12
) - end of group
$ - end of string
Just list all the valid numbers: ^(0|2|4|6|8|10|12)$. It's not the shortest solution but the most easy to read and understand.

Simple Regex with Date

I'm using Dreamweaver to replace about 1,000 instances of page titles that have a similar format:
5 5 2016 Nice tasty halibut
5 19 2016 A good king salmon and halibut day
...
I'd like the date to be formatted like:
5-5-2016 Nice tasty halibut
5-19-2016 A good king salmon and halibut day
I tried several ways of using Regular Expressions to fix this, but couldn't get the replaced value with the desired format. Can anyone help me out here?
Suggest using ([0-9]{1,2})\s+([0-9]{1,2})\s+([0-9]{4}).
This is a stricter regexpr. Essentially you are capturing 3 groups of numbers where
Group 1 must be digits and there can only be 1 or 2.
Group 2, same 1 or 2 digits.
Group 3, exactly 4 digits for the year.
Group 4, rest of the string.
And \s+ means 1 or more white spaces.
Then $1-$2-$3 $4 to match back all 4 groups together.
See:
https://regex101.com/r/wO3wD6/1
Search for ([0-9]+) ([0-9]+) (.*) and replace it with $1-$2-$3.

Regex to match some dates matching non-dates

I'm using some Regex to find date strings of the form Jan 12, 2015 or Feb 3, 1999.
The regex I'm using is \w+\s\d{1,2},\s\d{4} and it's working correctly, but the thing is that on the file are also some strings with the form:
Weg 58, 4047 or Strasse 1, 4482 and I also match them.
How can I avoid those non-date matches? My approach is:
The first string (the one of the month, Jan, Feb, etc.) has to have always length 3.
The year has to start with 1 or 2.
The thing is that I dont know how can I add these two options to my regex. Any help please?
You can make the test right here: https://regex101.com/r/bN2pO0/1
Thanks in advance.
Since the months won't change (ie: consistent values between January - Decemeber, we can put the 3 starting characters).
We can then use a OR | operator to select years starting with 1 or 2
/((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2},\s(1|2)\d{3})/ig
https://regex101.com/r/bN2pO0/3
Just as you used \d{1,2} to match a digit 1 or 2 times and \d{4} to match a digit 4 times, you can use \w{3} to match a word character 3 times.
For the year, you can use the pipe "or" operator |.
\w{3}\s\d{1,2},\s(?:1|2)\d{3}
Although, this will also match non-dates of form Abc xy, 1xyz
If you want, you can go with brute force approach or just get rid of regex and use code to capture the dates.
Brute force:
(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s[0-2]?[0-9],\s[12]\d{3}