正则表达式匹配以测试有效年份
给定一个值,我想验证它以检查它是否是有效的年份。我的标准很简单,值应该是带有 4
个字符的整数。我知道这不是最好的解决方案,因为它不允许 1000
之前的年份,而是允许 5000
等年份。这个标准足以满足我当前的情况。
我想出的是
\d{4}$
虽然这有效,但它也允许负值。
如何确保只允许使用正整数?
Given a value I want to validate it to check if it is a valid year. My criteria is simple where the value should be an integer with 4
characters. I know this is not the best solution as it will not allow years before 1000
and will allow years such as 5000
. This criteria is adequate for my current scenario.
What I came up with is
\d{4}$
While this works it also allows negative values.
How do I ensure that only positive integers are allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
年份从 1000 到 2999
对于 1900-2099
Years from 1000 to 2999
For 1900-2099
您需要添加一个起始锚点
^
,如下所示:您的正则表达式
\d{4}$
将匹配以 4 位数字结尾的字符串。因此像-1234
这样的输入将被接受。通过添加起始锚点,您将仅匹配那些开头和结尾为 4 位数字的字符串,这实际上意味着它们必须仅包含 4 位数字。
You need to add a start anchor
^
as:Your regex
\d{4}$
will match strings that end with 4 digits. So input like-1234
will be accepted.By adding the start anchor you match only those strings that begin and end with 4 digits, which effectively means they must contain only 4 digits.
这个问题的“公认”答案既不正确又短视。
这是不正确的,因为它将匹配像
0001
这样的字符串,它不是有效的年份。它是短视的,因为它不会匹配任何高于 9999 的值。我们是否已经忘记了 Y2K< /a>?相反,请使用正则表达式:
如果您需要匹配过去的年份,除了未来的年份之外,您还可以使用此正则表达式来匹配任何正整数:
即使您不期望过去的日期,您 也可以使用正则表达式来匹配任何正整数:无论如何,你可能想使用这个正则表达式,以防万一有人发明了时间机器并想要拿回你的软件。
注意:此正则表达式将匹配所有年份,包括 1 年之前的年份,因为它们通常用 BC 名称而不是负整数表示。当然,这种约定可能会在接下来的几千年中发生变化,因此您最好的选择是将任何整数(正数或负数)与以下正则表达式匹配:
The "accepted" answer to this question is both incorrect and myopic.
It is incorrect in that it will match strings like
0001
, which is not a valid year.It is myopic in that it will not match any values above 9999. Have we already forgotten the lessons of Y2K? Instead, use the regular expression:
If you need to match years in the past, in addition to years in the future, you could use this regular expression to match any positive integer:
Even if you don't expect dates from the past, you may want to use this regular expression anyway, just in case someone invents a time machine and wants to take your software back with them.
Note: This regular expression will match all years, including those before the year 1, since they are typically represented with a BC designation instead of a negative integer. Of course, this convention could change over the next few millennia, so your best option is to match any integer—positive or negative—with the following regular expression:
这适用于 1900 年至 2099 年:
This works for 1900 to 2099:
基于 @r92 答案,1970-2019 年:
Building on @r92 answer, for years 1970-2019:
要测试包含其他单词和年份的字符串中的年份,您可以使用以下正则表达式:\b\d{4}\b
To test a year in a string which contains other words along with the year you can use the following regex: \b\d{4}\b
理论上4位数的选项是正确的。但实际上,范围 1900-2099 可能会更好。
此外,它必须是非捕获组。许多评论和答案建议捕获分组,恕我直言,这是不正确的。因为对于匹配来说它可能有效,但是对于使用正则表达式提取匹配,它也会因为括号而提取 4 位数字和两位数字(19 和 20)。
这将适用于使用非捕获组的精确匹配:
(?:19|20)\d{2}
In theory the 4 digit option is right. But in practice it might be better to have 1900-2099 range.
Additionally it need to be non-capturing group. Many comments and answers propose capturing grouping which is not proper IMHO. Because for matching it might work, but for extracting matches using regex it will extract 4 digit numbers and two digit (19 and 20) numbers also because of paranthesis.
This will work for exact matching using non-capturing groups:
(?:19|20)\d{2}
使用;
1900 - 9999 年。
无需担心 9999 年及以后 - 到那时人工智能将完成所有编程!呵呵呵呵
您可以在 https://regex101.com/ 测试您的正则表达式
还有有关非捕获组的更多信息(提到在上面的评论中)这里 http://www.manifold.net/doc /radian/why_do_non-capture_groups_exist_.htm
Use;
for years 1900 - 9999.
No need to worry for 9999 and onwards - A.I. will be doing all programming by then !!! Hehehehe
You can test your regex at https://regex101.com/
Also more info about non-capturing groups ( mentioned in one the comments above ) here http://www.manifold.net/doc/radian/why_do_non-capture_groups_exist_.htm
你可以使用 [
^-]\d{4}$
之类的东西:你可以防止减号-
出现在 4 位数字之前。您还可以将
^\d{4}$
与^
一起使用来捕获字符串的开头。这实际上取决于你的场景...you can go with sth like [
^-]\d{4}$
: you prevent the minus sign-
to be before your 4 digits.you can also use
^\d{4}$
with^
to catch the beginning of the string. It depends on your scenario actually.../^\d{4}$/
这将检查字符串是否仅包含 4 个数字。在这种情况下,要输入年份 989,您可以改为 0989。
/^\d{4}$/
This will check if a string consists of only 4 numbers. In this scenario, to input a year 989, you can give 0989 instead.
您可以将整数转换为字符串。由于减号与数字不匹配,因此不会有负年份。
You could convert your integer into a string. As the minus sign will not match the digits, you will have no negative years.
我在Java中使用这个正则表达式
^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/ ](19|[2-9][0-9])[0-9]{2}$
适用于 1900 至 9999
I use this regex in Java
^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|[2-9][0-9])[0-9]{2}$
Works from 1900 to 9999
如果您需要匹配 YYYY 或 YYYYMMDD 您可以使用:
If you need to match YYYY or YYYYMMDD you can use:
您也可以使用这个。
You can also use this one.
就我而言,我想匹配一个以年份(4 位数字)结尾的字符串,例如:
它将返回
true
与此:上面的代码用于 Apps 脚本。
以下是测试上述正则表达式的链接:https://regex101.com/r/SzYQLN/1
In my case I wanted to match a string which ends with a year (4 digits) like this for example:
It'll return
true
with this one:The code above is used in Apps Script.
Here's the link to test the Regex above: https://regex101.com/r/SzYQLN/1
适用于 1950 至 2099 年,值为 4 个字符的整数
Works from 1950 to 2099 and value is an integer with 4 characters