正则表达式匹配以测试有效年份

发布于 2024-10-06 09:04:28 字数 242 浏览 10 评论 0原文

给定一个值,我想验证它以检查它是否是有效的年份。我的标准很简单,值应该是带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

无言温柔 2024-10-13 09:04:28

年份从 1000 到 2999

^[12][0-9]{3}$

对于 1900-2099

^(19|20)\d{2}$

Years from 1000 to 2999

^[12][0-9]{3}$

For 1900-2099

^(19|20)\d{2}$
荆棘i 2024-10-13 09:04:28

您需要添加一个起始锚点 ^,如下所示:

^\d{4}$

您的正则表达式 \d{4}$ 将匹配以 4 位数字结尾的字符串。因此像 -1234 这样的输入将被接受。

通过添加起始​​锚点,您将仅匹配那些开头和结尾为 4 位数字的字符串,这实际上意味着它们必须仅包含 4 位数字。

You need to add a start anchor ^ as:

^\d{4}$

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.

余厌 2024-10-13 09:04:28

这个问题的“公认”答案既不正确又短视。

这是不正确的,因为它将匹配像 0001 这样的字符串,它不是有效的年份。

它是短视的,因为它不会匹配任何高于 9999 的值。我们是否已经忘记了 Y2K< /a>?相反,请使用正则表达式:

^[1-9]\d{3,}$

如果您需要匹配过去的年份,除了未来的年份之外,您还可以使用此正则表达式来匹配任何正整数:

^[1-9]\d*$

即使您不期望过去的日期,您 也可以使用正则表达式来匹配任何正整数:无论如何,你可能想使用这个正则表达式,以防万一有人发明了时间机器并想要拿回你的软件。

注意:此正则表达式将匹配所有年份,包括 1 年之前的年份,因为它们通常用 BC 名称而不是负整数表示。当然,这种约定可能会在接下来的几千年中发生变化,因此您最好的选择是将任何整数(正数或负数)与以下正则表达式匹配:

^-?[1-9]\d*$

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:

^[1-9]\d{3,}$

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:

^[1-9]\d*$

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:

^-?[1-9]\d*$
丿*梦醉红颜 2024-10-13 09:04:28

这适用于 1900 年至 2099 年:

/(?:(?:19|20)[0-9]{2})/

This works for 1900 to 2099:

/(?:(?:19|20)[0-9]{2})/
瀟灑尐姊 2024-10-13 09:04:28

基于 @r92 答案,1970-2019 年:

(19[789]\d|20[01]\d)

Building on @r92 answer, for years 1970-2019:

(19[789]\d|20[01]\d)
黎夕旧梦 2024-10-13 09:04:28

要测试包含其他单词和年份的字符串中的年份,您可以使用以下正则表达式:\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

傲性难收 2024-10-13 09:04:28

理论上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}

Spring初心 2024-10-13 09:04:28

使用;

^(19|[2-9][0-9])\d{2}$ 

1900 - 9999 年。

无需担心 9999 年及以后 - 到那时人工智能将完成所有编程!呵呵呵呵

您可以在 https://regex101.com/ 测试您的正则表达式

还有有关非捕获组的更多信息(提到在上面的评论中)这里 http://www.manifold.net/doc /radian/why_do_non-capture_groups_exist_.htm

Use;

^(19|[2-9][0-9])\d{2}$ 

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

橘香 2024-10-13 09:04:28

你可以使用 [^-]\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...

避讳 2024-10-13 09:04:28

/^\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.

_畞蕅 2024-10-13 09:04:28

您可以将整数转换为字符串。由于减号与数字不匹配,因此不会有负年份。

You could convert your integer into a string. As the minus sign will not match the digits, you will have no negative years.

々眼睛长脚气 2024-10-13 09:04:28

我在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

沩ん囻菔务 2024-10-13 09:04:28

如果您需要匹配 YYYY 或 YYYYMMDD 您可以使用:

^((?:(?:(?:(?:(?:[1-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:[2468][048]|[13579][26])00))(?:0?2(?:29)))|(?:(?:[1-9]\d{3})(?:(?:(?:0?[13578]|1[02])(?:31))|(?:(?:0?[13-9]|1[0-2])(?:29|30))|(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8])))))|(?:19|20)\d{2})$

If you need to match YYYY or YYYYMMDD you can use:

^((?:(?:(?:(?:(?:[1-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:[2468][048]|[13579][26])00))(?:0?2(?:29)))|(?:(?:[1-9]\d{3})(?:(?:(?:0?[13578]|1[02])(?:31))|(?:(?:0?[13-9]|1[0-2])(?:29|30))|(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8])))))|(?:19|20)\d{2})$
请止步禁区 2024-10-13 09:04:28

您也可以使用这个。

([0-2][0-9]|3[0-1])\/([0-1][0-2])\/(19[789]\d|20[01]\d)

You can also use this one.

([0-2][0-9]|3[0-1])\/([0-1][0-2])\/(19[789]\d|20[01]\d)
巴黎盛开的樱花 2024-10-13 09:04:28

就我而言,我想匹配一个以年份(4 位数字)结尾的字符串,例如:

Oct 2020
Nov 2020
Dec 2020
Jan 2021

它将返回 true 与此:

var sheetName = 'Jan 2021';
var yearRegex = new RegExp("\b\d{4}$");
var isMonthSheet = yearRegex.test(sheetName);
Logger.log('isMonthSheet = ' + isMonthSheet);

上面的代码用于 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:

Oct 2020
Nov 2020
Dec 2020
Jan 2021

It'll return true with this one:

var sheetName = 'Jan 2021';
var yearRegex = new RegExp("\b\d{4}
quot;);
var isMonthSheet = yearRegex.test(sheetName);
Logger.log('isMonthSheet = ' + isMonthSheet);

The code above is used in Apps Script.

Here's the link to test the Regex above: https://regex101.com/r/SzYQLN/1

乱世争霸 2024-10-13 09:04:28

您可以尝试以下方法从字符串中捕获有效年份:

.*(19\d{2}|20\d{2}).*

You can try the following to capture valid year from a string:

.*(19\d{2}|20\d{2}).*
东风软 2024-10-13 09:04:28

适用于 1950 至 2099 年,值为 4 个字符的整数

^(?=.*?(19[56789]|20\d{2}).*)\d{4}$

Works from 1950 to 2099 and value is an integer with 4 characters

^(?=.*?(19[56789]|20\d{2}).*)\d{4}$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文