[英]Using regex to find certain range
I'm trying to use regex to find a range between 1-31. 我正在尝试使用正则表达式查找1-31之间的范围。 This is what i have so far but its not working. 这是我到目前为止所没有的,但是没有用。 Also could someone please explain to me how the regex formatting works? 还可以有人给我解释一下正则表达式格式是如何工作的吗?
1[0-3]?|2[0-9]|[1-9]
像这样分别考虑1-9、10-29和30-31情况。
[1-9]|[12][0-9]|3[01]
Some people, when confronted with a problem, think "I know, I'll use regular expressions." 有些人在遇到问题时会认为“我知道,我会使用正则表达式”。 Now they have two problems. 现在他们有两个问题。
-- Jamie Zawinski -杰米·扎温斯基(Jamie Zawinski)
Here's the verbose version, which also throws up two other useful functions: 这是详细版本,它还引发了另外两个有用的功能:
... {
static public int asInt(String s, int dflt) {
try {
return parseInt(s, 10);
} catch (NumberFormatException nfe) {
return dflt;
}
}
static public boolean isInRange(int n, int mn, int mx) {
return (n >= mn) && (n <= mx);
}
static public boolean isBetween1and31(String s) {
return isInRange(asInt(s, 0), 1, 31);
}
}
([1-9]|[12][0-9]|3[01])
http://gamon.webfactional.com/regexnumericrangegenerator/ http://gamon.webfactional.com/regexnumericrangegenerator/
This would be enough to generate a regex b/wa range. 这将足以生成正则表达式b / wa范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.