Dynamic regex for date formats

Is there an existing solution for dynamically creating regular expressions from a given date format template? The supported date format format does not matter (Joda DateTimeFormat, java.text.SimpleDateTimeFormat or others).

i.e. for a specific date and time format (for example, "dd / MM / yyyy hh: mm"), it will generate the appropriate regular expression to match the time dates in the specified formats.

+4
source share
4 answers

I assume that you have a limited alphabet from which your temporary formats can be created. This means that "HH" will always be “hours” on a 24-hour clock, "dd" always a day with a start zero, etc.

Due to the consistent nature of the time format, you can try tokenize a string of the format "dd/mm/yyyy HH:nn" into the array ["dd", "/", "mm", "/", "yyyy", " ", "HH", ":", "nn"] . Then go and form a template string from this array, replacing "HH" with "([01][0-9]|2[0-3])" and so on. Predesign these template atoms into a lookup table / array. All parts of your array that are not in the lookup table are literals. Select them according to the rules of regular expressions and add them to the template line.


EDIT: as a side effect of a regular expression solution, when you put all the "regular expressions" of your lookup table in parsers and track their order in a given format string, you can use subheadings to extract the necessary components from the match and feed them into a function CreateDate, thereby completely skipping the ParseDate part.

+3
source

If you are looking for a basic date check. this code matches this data.

 \b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\b 10/07/2008 10.07.2008 1-01/2008 10/07/08 10.07.2008 1-01/08 

Code via regexbuddy

+1
source

SimpleDateFormat already does this with the parse() method.

If you need to parse multiple dates from a single line, start with a regular expression (even if it matches too condescendingly) and use parse() for all possible matches found by the regular expression.

0
source

Below is the js / jQuery code for dynamically generating a regular expression for the date format Only, not for the date Time (the development version has not yet been fully tested.)

Date format must be in "DMY"

eg.
DD-MM-GG,
DD-MM-YYYY,
YYYY-MM-DD,
YYYY-DD-MM,
MM-DD-YYYY,
MM-DD-GG,
DD / MM / YY,
DD / MM / YYYY,
YYYY / MM / DD,
YYYY / DD / MM,
MM / DD / YYYY,
MM / DD / YY
Or other formats, but created with the symbol [DMY]

 var dateFormat = "DD-MM-YYYY"; var order = []; var position = {"D":dateFormat.search('D'),"M":dateFormat.search('M'),"Y":dateFormat.search('Y')}; var count = {"D":dateFormat.split("D").length - 1,"M":dateFormat.split("M").length - 1,"Y":dateFormat.split("Y").length - 1}; var seprator =''; for(var i=0; i<dateFormat.length; i++){ if(["Y","M","D"].indexOf(dateFormat.charAt(i))<0){ seprator = dateFormat.charAt(i); }else{ if(order.indexOf(dateFormat.charAt(i)) <0 ){ order.push(dateFormat.charAt(i)); } } } var regEx = "^"; $(order).each(function(ok,ov){ regEx += '(\d{'+count[ov]+'})'+seprator; }); regEx = regEx.substr(0,(regEx.length)-1); regEx +="$"; var re = new RegExp(regEx); console.log(re); 

NOTE. No validation checks for months / days for example. the month should be 01-12 or the date should be 01-31

0
source

Source: https://habr.com/ru/post/1276610/


All Articles