年份序列的正则表达式

发布于 2024-12-11 20:04:38 字数 288 浏览 0 评论 0原文

所以我试图匹配类似

2011,2012,2013

2011,

2011 

但不是:

2011,2012011,201,2012

我尝试使用 ([0-9]{ 4},?([0-9]{4},?)*) 但如果第一年匹配,则不会考虑其余年份。

So I'm trying to match something like

2011,2012,2013

or

2011,

or

2011 

but NOT:

2011,201 or 2011,201,2012

I tried with ([0-9]{4},?([0-9]{4},?)*) but if the first year is matched, it does not consider the rest .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

倾`听者〃 2024-12-18 20:04:38

你很接近。

 ^[0-9]{4}(?:,[0-9]{4})*,?$

这将匹配由 4 位数字和逗号的重复序列组成的任何字符串。

^$ 分别匹配字符串的开头和结尾。因此,仅当字符串仅由这些元素组成时,它才会匹配。

(?:) 是非捕获组。它允许您创建重复组,而无需将所有组存储到变量中。

编辑:忘记了末尾的可选逗号。添加了 ,? 来处理它。

编辑2:根据FailedDev的建议,这是我最初的想法。它也有效,但我认为它更难理解。它更聪明,但这并不总是一件好事。

^(:?[0-9]{4}(?:,|$))+$

You were close.

 ^[0-9]{4}(?:,[0-9]{4})*,?$

That will match any string consisting of a repeating sequence of 4-digit numbers and commas.

The ^ and $ match the beginning and end of the string, respectively. Thus, it will only match if the string consists of only those elements.

The (?:) is a non-capture group. It allows you to create repeating groups without storing all of them into variables.

EDIT: Forgot about the optional comma at the end. Added a ,? to take care of it.

EDIT 2: At FailedDev's advice, here was my original idea. It also works, but I think it is harder to understand. It is more clever, but that's not always a good thing.

^(:?[0-9]{4}(?:,|$))+$
ㄟ。诗瑗 2024-12-18 20:04:38
if (subject.match(/^\d{4}(?:,?|(?:,\d{4}))+$/)) {
    // Successful match
}

这应该有效。

说明:

"^" +              // Assert position at the beginning of the string
"\\d" +             // Match a single digit 0..9
   "{4}" +            // Exactly 4 times
"(?:" +            // Match the regular expression below
   "|" +              // Match either the regular expression below (attempting the next alternative only if this one fails)
      "," +              // Match the character “,” literally
         "?" +              // Between zero and one times, as many times as possible, giving back as needed (greedy)
   "|" +              // Or match regular expression number 2 below (the entire group fails if this one fails to match)
      "(?:" +            // Match the regular expression below
         "," +              // Match the character “,” literally
         "\\d" +             // Match a single digit 0..9
            "{4}" +            // Exactly 4 times
      ")" +
")+" +             // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"$"                // Assert position at the end of the string (or before the line break at the end of the string, if any)
if (subject.match(/^\d{4}(?:,?|(?:,\d{4}))+$/)) {
    // Successful match
}

This should work.

Explanation :

"^" +              // Assert position at the beginning of the string
"\\d" +             // Match a single digit 0..9
   "{4}" +            // Exactly 4 times
"(?:" +            // Match the regular expression below
   "|" +              // Match either the regular expression below (attempting the next alternative only if this one fails)
      "," +              // Match the character “,” literally
         "?" +              // Between zero and one times, as many times as possible, giving back as needed (greedy)
   "|" +              // Or match regular expression number 2 below (the entire group fails if this one fails to match)
      "(?:" +            // Match the regular expression below
         "," +              // Match the character “,” literally
         "\\d" +             // Match a single digit 0..9
            "{4}" +            // Exactly 4 times
      ")" +
")+" +             // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"$"                // Assert position at the end of the string (or before the line break at the end of the string, if any)
沫离伤花 2024-12-18 20:04:38

这样就可以解决问题了...

 /^[0-9]{4}(,[0-9]{4})*,?$/

即 4 位数字后跟零个或多个(逗号后跟 4 位数字),并且可以选择在结尾之前加上最后一个(难看的)逗号。

第一个 ^ 和最后一个 $ 字符确保字符串中不能出现任何其他内容。

This will do the trick...

 /^[0-9]{4}(,[0-9]{4})*,?$/

i.e. 4 digits followed by zero or more of (a comma followed by 4 digits) and optionally a last (bad looking) comma before the end.

The first ^ and last $ chars ensure that nothing else can be present in the string.

梨涡 2024-12-18 20:04:38
/^(?:\d{4},?)*$/

检查四位数字,每个数字可能后跟一个逗号,0 到多次

/^(?:\d{4},?)*$/

Checks for four digits, each possibly followed by a comma, 0 to many times

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文