动态正则表达式日期时间格式
现有解决方案是在给定的日期格式模式之外动态创建正则表达式吗?支持的日期时间格式模式无关紧要(Joda DateTimeFormat,java.text.SimpleDateTimeFormat或其他)。
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).
即对于给定的日期时间格式(例如dd / MM / yyyy hh:mm),它将生成相应的正则表达式以匹配指定格式的日期时间。
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.
我想你有一个有限的字母表,你的时间格式可以被构建。这意味着HH
将始终是24小时制的小时,dd
总是一天与前导零,等等。
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.
由于时间格式的顺序性质,您可以尝试将格式字符串标记为 dd / mm / yyyy HH:nn
into a array [dd,/,mm,/,yyyy, HH,:,nn]
。然后,通过将HH
替换为([01] [0-9] | 2 [0],然后从该数组中形成一个模式字符串-3])
等等。将这些模式原子预构建到查找表/数组中。数组中不在查找表中的所有部分都是文字。将它们转换为正则表达式,并将其附加到模式字符串。
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.
编辑:作为基于正则表达式的副作用解决方案,当您将查找表的所有正则表达式原子放入括号中并按照给定的格式字符串跟踪其顺序时,您将能够使用子匹配从匹配中提取所需的组件并将其提供给CreateDate函数,从而完全跳过ParseDate部分。
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.