简体   繁体   English

朱利安日正则表达没有领先的零

[英]Julian Day Regex Without Leading Zeros

I am really struggling with creating a Reg Ex for Julian Day that does not allow leading zeros. 我真的很难为朱利安日创建一个不允许前导零的Reg Ex。

I am using it for Input Validation using JavaScript Reg Ex. 我使用JavaScript Reg Ex将它用于输入验证。 It needs to match 1 through 366. 它需要匹配1到366。

Example Matches: 示例匹配:

  • 1 1
  • 99 99
  • 366 366
  • 159 159

Example Match Failures: 示例匹配失败:

  • 01 01
  • 001 001
  • 099 099
  • 367 367
  • 999 999
  • 0 0

I tried this on regex101: 我在regex101上尝试过这个:

^[1-9]|[1-9][0-9]|[1-3][0-5][0-9]|36[0-6]$ ^ [1-9] | [1-9] [0-9] | [1-3] [0-5] [0-9] | 36 [0-6] $

But for I am not getting the optional parts down right. 但是因为我没有正确选择可选部件。 So when I put in 266, I get a match on 2 and 66. (This issue is translating to my input validation control .) 所以当我输入266时,我得到了2和66的匹配。(这个问题正在转换为我的输入验证控件 。)

I thought about trying to use + for one or more, but I need to not allow leading zeros, so that does not work. 我想过尝试将+用于一个或多个,但我不需要允许前导零,所以这不起作用。

I have read the guidance on asking a RegEx question and tried to follow it, but if I missed something, please let me know and I will update my question. 我已经阅读了有关询问RegEx问题的指导并试图遵循它,但如果我错过了某些内容,请告诉我,我会更新我的问题。

The main issues are two: 1) the alternatives should have been grouped so that ^ and $ anchors could be applied to all of them, 2) the [1-3][0-5][0-9] part did not match 160 to 199 and 260 to 299 , this part should have been split into two separate branches, [12][0-9]{2}|3[0-5][0-9] . 主要问题是两个:1)替代方案应该被分组,以便^$锚点可以应用于所有这些,2) [1-3][0-5][0-9]部分不匹配160199260299 ,该部分应该被分成两个单独的分支, [12][0-9]{2}|3[0-5][0-9]

You may use 你可以用

^(?:[1-9]|[1-9][0-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-6])$

See the regex demo . 请参阅正则表达式演示

Details 细节

  • ^ - start of string ^ - 字符串的开头
  • (?: - group of alternatives: (?: - 替代方案组:
    • [1-9] - 1 to 9 [1-9] - 1到9
    • | - or - 要么
    • [1-9][0-9] - 10 to 99 [1-9][0-9] - 1099
    • | - or - 要么
    • [12][0-9]{2} - 100 to 299 [12][0-9]{2} - 100299
    • | - or - 要么
    • 3[0-5][0-9] - 300 to 359 3[0-5][0-9] - 300359
    • | - or - 要么
    • 36[0-6] - 360 to 366 36[0-6] - 360366
  • ) - end of the alternation group ) - 交替组的结束
  • $ - and the end of the string. $ - 和字符串的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM