问题
Is there an existing solution to create regular expressions dynamically out of given date time format pattern? Supported date time format pattern does not matter (Joda DateTimeFormat, java.text.SimpleDateTimeFormat or others).
i.e. for a given date-time format (for example "dd/MM/yyyy hh:mm"), it will generate corresponding regular expression to match the date-times within the specified formats.
回答1:
I guess you have a limited alphabet that your time formats can be constructed of. That means, "HH"
would always be "hours" on the 24-hour clock, "dd"
always the day with leading zero, and so on.
Because of the sequential nature of a time format, you could try to tokenize a format string of "dd/mm/yyyy HH:nn"
into an array ["dd", "/", "mm", "/", "yyyy", " ", "HH", ":", "nn"]
. Then go ahead and form a pattern string from that array by replacing "HH"
with "([01][0-9]|2[0-3])"
and so on. Preconstruct these pattern atoms into a lookup table/array. All parts of your array that are not in the lookup table are literals. Escape them to according regex rules and append them to you pattern string.
EDIT: As a side effect for a regex based solution, when you put all regex "atoms" of your lookup table into parens and keep track of their order in a given format string, you would be able to use sub-matches to extract the required components from a match and feed them into a CreateDate function, thus skipping the ParseDate part altogether.
回答2:
If your looking for basic date checking. 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
回答3:
SimpleDateFormat
already does this with the parse()
method.
If you need to parse multiple dates from a single string, start with a regex (even if it matches too leniently), and use parse()
on all the potential matches found by the regex.
回答4:
Below given js/jQuery code for Dynamic generate regex for Date format Only, not for date Time (Development version not fully tested yet. )
Date Format should be in " D M Y "
eg.
DD-MM-YY,
DD-MM-YYYY,
YYYY-MM-DD,
YYYY-DD-MM,
MM-DD-YYYY,
MM-DD-YY,
DD/MM/YY,
DD/MM/YYYY,
YYYY/MM/DD,
YYYY/DD/MM,
MM/DD/YYYY,
MM/DD/YY
Or Other Formats but created with [ D M Y ] Character
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- There is no validation check for months / days
eg. month should be in 01-12 Or date should be in 01-31
来源:https://stackoverflow.com/questions/52066/dynamic-regex-for-date-time-formats