Java - 正则表达式 - 需要 1-12 个月
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10260148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Regex - Months requiring 1-12
提问by Tim
I'm trying to build a regex for a field containing the number of months, of course, this needs to be only numbers 1-12. However, new to regex this is a bit new to me and I just need to check I'm right. It seems to work alright. Although, am I correct in assuming the range here 1-12 allows any number in this range, I only ask as I've only ever seen it done with 0-9 before.
我正在尝试为包含月数的字段构建一个正则表达式,当然,这只需数字 1-12。但是,正则表达式新手这对我来说有点新,我只需要检查我是对的。它似乎工作正常。虽然,我假设这里的范围 1-12 允许这个范围内的任何数字是正确的,但我只是问,因为我以前只见过 0-9 完成它。
[1-12]{1,2}
回答by Murtnowski
This might do. Two digits or one digit
这可能会。两位数或一位数
1[0-2]|[1-9]
Edit: If you're worried about numbers like 55 use this:
编辑:如果您担心像 55 这样的数字,请使用:
^(1[0-2]|[1-9])$
Adding the ^
and $
means start and end of string. Use https://regex101.comas a good learning tool.
添加^
和$
表示字符串的开始和结束。使用https://regex101.com作为一个很好的学习工具。
回答by Matt Ball
Don't just a regex. Just don't. Parse the string to an int
, and check that the value is in range.
不要只是一个正则表达式。只是不要。将字符串解析为int
,并检查该值是否在范围内。
boolean isMonthInRange(String monthInput)
{
try
{
int value = Integer.parseInt(monthInput, 10);
return value >= 1 && value <= 12;
}
catch (NumberFormatException e)
{
return false;
}
}
Since this is Java, there are a whole pile of libraries we could throw at this problem instead of rolling a custom solution. At the very least, don't use magic numbers (1
and 12
) in the condition.
由于这是 Java,因此我们可以抛出一大堆库来解决这个问题,而不是推出自定义解决方案。至少,不要在条件中使用幻数(1
和12
)。
回答by Joanna Derks
Another suggestion:
另一个建议:
\b(?:1[0-2]|[1-9])\b
It's possible that you don't need to check the word boundaries if you allow just the full match - but otherwise the regexp. in previous answers would match e.g. 1 and 3 in 13 as two separate matches.
如果您只允许完全匹配,则可能不需要检查单词边界——否则需要检查正则表达式。在以前的答案中将匹配例如 13 中的 1 和 3 作为两个单独的匹配项。
回答by Taste
I think this should do fine: (^[1-9]$)|(^0[1-9]|1[0-2]$)
我认为这应该没问题: (^[1-9]$)|(^0[1-9]|1[0-2]$)
you can insert 1-12 and also 01, 02 ...
您可以插入 1-12 和 01, 02 ...
回答by Garrett Albright
/(1[0-2]|[1-9])/
Though as others have said, there are probably better ways. Just checking if the value, when evaluated as an integer, is < 1 or > 12 would work as well (and be simpler for those unfamiliar with regular expressions to understand). (Edit: Like MДΓΓ БДLL's answer.)
尽管正如其他人所说,可能有更好的方法。只需检查作为整数计算的值是否 < 1 或 > 12 也可以(并且对于不熟悉正则表达式的人来说更容易理解)。(编辑:就像 MДΓΓ БДLL 的回答一样。)
回答by ControlAltDel
Here's what I'd do:
这是我要做的:
[1-9]|10|11|12
回答by Mac
And for an HTML/JavaScript solution, the following regex could be used for an HTML input pattern attribute, to enforce mm/dd/yyyy format, where mm is 1-12, dd is 1-31, and yyyy is 1930-1999...
对于 HTML/JavaScript 解决方案,以下正则表达式可用于 HTML 输入模式属性,以强制执行 mm/dd/yyyy 格式,其中 mm 为 1-12,dd 为 1-31,yyyy 为 1930-1999。 ..
(?:1[0-2]|[1-9])\/(?:[1-9]|1[0-9]|2[0-9]|3[0-1])\/(19[3-9]{1}[0-9]{1})
...and, along with some CSS to provide visual cues, it might be used like this:
...并且,连同一些 CSS 来提供视觉提示,它可以像这样使用:
input::placeholder {
color: rgb(46, 163, 242);
font-style: italic;
}
input:focus {
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.4) inset;
}
input:required:invalid, input:focus:invalid {
border: 2px solid rgb(255, 159, 159) !important;
background: white;
}
input:required:valid {
background: rgba(196, 255, 214, .5) !important;
color: black !important;
}
section.form-layout input {
width: 120px;
height: 27px;
border: 2px solid rgb(46, 163, 242);
border-radius: 5px;
text-align: center;
color: rgb(46, 163, 242);
transition: all .5s ease;
}
*:focus {
outline: none;
}
<section class="form-layout">
<p>Enter your DOB <span style="font-style: italic;">(mm/dd/yyyy)</span>:</p>
<input required id="dob-1" placeholder="Date of Birth" type="text" pattern="(?:1[0-2]|[1-9])\/(?:[1-9]|1[0-9]|2[0-9]|3[0-1])\/(19[3-9]{1}[0-9]{1})" autocomplete="off">
</section>