我目前在一个铁路项目中工作,最后我遇到了一个与铁路限制相符的问题。我的田地应该只接受一个带有类似日期格式的字符串,就像这个格式:
10-25-2012 5:00PM
这在使用 Regex 的匹配限制中是可能的吗? 我总是很难用 regex 进行数据过滤, 这使得它有点混乱 。
我目前在一个铁路项目中工作,最后我遇到了一个与铁路限制相符的问题。我的田地应该只接受一个带有类似日期格式的字符串,就像这个格式:
10-25-2012 5:00PM
这在使用 Regex 的匹配限制中是可能的吗? 我总是很难用 regex 进行数据过滤, 这使得它有点混乱 。
如果是数据,为什么不使用标准的事项日期来验证它?例如:
static constraints = {
mydate validator: {
try {
Date.parse( MM-dd-yyyy hh:mma , it)
return true
} catch (ParseException e) {
return false
}
}
}
Btw, 在此情况下, Date
可以分析无效的日期( 并将其转换为correnct 值, 如 15am
改为 3pm
。 如果您需要完全有效的格式, 您可以将其与原始值比较 :
static constraints = {
mydate validator: {
try {
Date date = Date.parse( MM-dd-yyyy hh:mma , it)
return Date.format( MM-dd-yyyy hh:mma , date) == it
} catch (ParseException e) {
return false
}
}
}
或者您可以使用 SustainDate Format
代替 :
final static DateFormat DATEFORMAT = new SimpleDateFormat( MM-dd-yyyy hh:mma )
static constraints = {
mydate validator: {
try {
Date date = DATEFORMAT.parse(it)
return DATEFORMAT.format(date) == it
} catch (ParseException e) {
return false
}
}
}
没有日期对象可以使用吗?
构造正则不难, 特别是您要直线前进 :
^d{2}-d{2}-d{4} d{2}:d{2}[AP]M$
匹配字符串的起始
$$
匹配字符串的结尾
d
是位数
是一个量化符, 使先前的字符需要两次
[AP]
是一个匹配 A
或 P
的字符类。
< 加强> 此正则仅检查格式, 如果数字代表有效日期或时间! 强 > (例如, 99- 99- 0000 35: 61PM) 则不检查格式 。
阅读我的博客文章"http://blog.xisb.de/?p=4" rel="nofollow"。"每个程序员对于一些更简短的信息应该知道的常规表达方式 是什么?
尝试此选项 : )}? ((AMPM)$
^ Start of string
(0[1-9]|1[012]) Month
-
(0[1-9]|[12][0-9]|3[01]) Day
-
((?:19|20)dd) Year
([0-1]?[0-9]|2[0-4]) HH
:
([0-5][0-9]) MM
(?::([0-5][0-9]))? optional :SS
(AM|PM)
$ End of string
它捕捉到月、日、年、小时、分钟、秒和AM/MM。
“强势”编辑:如Vikas指出的,它不检查每个月有多少天的时间。
< streng> 尝试此 streng>
(?:(?:(?:0?[469]|11)-(?:0?(?:[1-9]|[12][0-9])|30)-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))|(?:(?:0?[13578]|1[02])-(?:0?(?:[1-9]|[12][0-9])|3[01])-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))|(?:(?:0?2)-(?:0?(?:[1-9]|1[0-9])|2[0-8])-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))) +(?:(?:(?:0?([0-9])|1[0-2])):(?:0?([0-9])|[1-5][0-9])(?:[AP]M))
或
(?:(?:(?:0?[469]|11)-(?:0?(?:[1-9]|[12][0-9])|30)-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))|(?:(?:0?[13578]|1[02])-(?:0?(?:[1-9]|[12][0-9])|3[01])-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))|(?:(?:0?2)-(?:0?(?:[1-9]|1[0-9])|2[0-9])-(?:1[7-9][0-9]{2}|200[0-9]|201[0-2]))) +(?:(?:(?:0?([0-9])|1[0-2])):(?:0?([0-9])|[1-5][0-9])(?:[AP]M))
Note: Two patterns both have a common limitation that it would match invalid dates
if the month
is february
. If-else conditionals are accepted in some RegEx flavors but there is no such functions exist f或arithmatic operations. May be in far 或near future this could be implemented in some RegEx flavors, but I think this not the case f或what RegEx is basically is.
28
from month of February. And it would never match any invalid date.29
.`他们'我众所周知,这个问题不可能通过使用RegEx 来解决。
Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.
I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />
How do I, using preg_replace, replace more than one underscore with just one underscore?
I was trying to write a regexp to replace all occurrences of with unless the is already preceeded immediately by a . I m doing this in Ruby 1.8.6 which doesn t support look behind in ...
Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...
I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...
I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...
I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...