简体   繁体   English

如何创建正则表达式检查罗马数字?

[英]How to create regular expression checking Roman numerals?

I need to create regular expression which verifies if user inputs: 我需要创建正则表达式来验证用户是否输入:

  • 4 digits OR 4位数 OR
  • value like XXXXXX-YY , where X is roman numerals from I to XXXIII and YY is two latin characters (AZ) XXXXXX-YY这样的值,其中X是从I到XXXIII的罗马数字,YY是两个拉丁字符(AZ)

According to the requirements, these are possible roman number-formats. 根据要求,这些是可能的罗马数字格式。 For readability, only the maximum number of X is shown. 为了便于阅读,仅显示最大数量的X.

 XXX III (or: <empty>, I or II instead of III) XX V (or: IV, IX and X instead of IV) 

I suggest this compact pattern: 我建议这个紧凑的模式:

/^(\d{4}|(?=[IVX])(X{0,3}I{0,3}|X{0,2}VI{0,3}|X{0,2}I?[VX])-[A-Z]{2})$/i

Explanation: 说明:

^                Begin of string
(                Begin of group 1.
  \d{4}             4 digits

|                 OR

  (?=[IVX])         Look-ahead: Must be followed by a I, V or X
  (                  Begin of group 2.
     X{0,3}I{0,3}       = 0 1 2 3  + { 0 ; 10 ; 20 ; 30} (roman)
  |                  OR
     X{0,2}VI{0,3}      = 5 6 7 8  + { 0 ; 10 ; 20 }     (roman)
  |                  OR
     X{0,2}I?[VX]       = 4 9      + { 0 ; 10 ; 20 }     (roman)
  )                  End of group 2
  -[A-Z]{2}          Postfixed by a hyphen and two letters
)                 End of group 1.
$                End of string

Well the part that matches a Roman numeral between I and XXXIII is: 那么匹配I和XXXIII之间的罗马数字的部分是:

(?:X(?:X(?:V(?:I(?:I?I)?)?|X(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)?|V(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)?|V(?:I(?:I?I)?)?|I(?:[VX]|I?I)?)

As revealed by this: 据透露:

#!/usr/bin/env perl
use Regexp::Assemble;
use Roman;

my $ra = new Regexp::Assemble;

for my $num (1..33) {
    $ra->add(Roman($num));
} 

print $ra->re, "\n";
function inputIsValid(value) {
    var r = /(^[0-9]{4}$)|(^(?:(?:[X]{0,2}(?:[I](?:[XV]?|[I]{0,2})?|(?:[V][I]{0,3})?))|(?:[X]{3}[I]{0,3}))\-[A-Z]{2}$)/ig;
    return value.match(r);
}

That will match either a 4-digit input, or a roman number (ranged 1 - 33) followed by a dash and two letters. 这将匹配4位数输入或罗马数字(范围1 - 33),后跟短划线和两个字母。

To explain the regex, below is an expanded source with comments: 为了解释正则表达式,下面是一个带注释的扩展源:

// Test for a 4-digit number
(                                       // Start required capturing group
    ^                                   // Start of string
    [0-9]{4}                            // Test for 0-9, exactly 4 times
    $                                   // End of string
)                                       // End required capturing group
|                                       // OR
// Test for Roman Numerals, 1 - 33, followed by a dash and two letters
(                                       // Start required capturing group
    ^                                   // Start of string
    (?:                                 // Start required non-capturing group
        // Test for 1 - 29
        (?:                             // Start required non-capturing group
            // Test for 10, 20, (and implied 0, although the Romans did not have a digit, or mathematical concept, for 0)
            [X]{0,2}                    // X, optionally up to 2 times
            (?:                         // Start required non-capturing group
                // Test for 1 - 4, and 9
                [I]                     // I, exactly once (I = 1)
                (?:                     // Start optional non-capturing group
                    // IV = 4, IX = 9
                    [XV]?               // Optional X or V, exactly once
                    |                   // OR
                    // II = 2, III = 3
                    [I]{0,2}            // Optional I, up to 2 times
                )?                      // End optional non-capturing group
                |                       // OR
                // Test for 5 - 8
                (?:                     // Start optional non-capturing group
                    [V][I]{0,3}         // Required V, followed by optional I, up to 3 times
                )?                      // End optional non-capturing group
            )                           // End required non-capturing group
        )                               // End required non-capturing group
        |                               // OR
        // Test for 30 - 33
        (?:                             // Start required non-capturing group
            // Test for 30
            [X]{3}                      // X exactly 3 times
            // Test for 1 - 3
            [I]{0,3}                    // Optional I, up to 3 times
        )                               // End required non-capturing group
    )                                   // End required non-capturing group
    // Test for dash and two letters
    \-                                  // Literal -, exactly 1 time
    [A-Z]{2}                            // Alphabetic character, exactly 2 times
    $                                   // End of string
)                                       // End required capturing group

The 4-digit number and trailing \\-[AZ]{2} were (to me) self-evident. 4位数字和尾随\\-[AZ]{2}对我来说是不言而喻的。 My method for the Roman Numerals was to: 我对罗马数字的方法是:

  1. Open Excel Populate a column with 1-33. 打开Excel使用1-33填充列。
  2. Convert that column to Roman Numerals (in all 7 different varieties). 将该列转换为罗马数字(所有7种不同的变体)。
  3. Check to see if any of the varieties were different from 1-33 (they weren't). 检查是否有任何品种与1-33不同(它们不是)。
  4. Fiddled with moving the Roman Numerals into the minimum number of unique patterns that limited them to 33 (ie, "then shalt thou count to thirty-three, no more, no less. Thirty-three shall be the number thou shalt count, and the number of the counting shall be thirty-three. Thirty-four shalt thou not count, neither count thou thirty-two, excepting that thou then proceed to thirty-three. Thirty-five is right out.") 摆弄将罗马数字移动到最小数量的独特模式,将它们限制在33(即“然后你将数到三十三,不多,不少。三十三是你要数的数字,而且计数的数量应该是三十三。三十四你不算数,不算三十二,除了你然后进入三十三。三十五是正确的。“)
  5. Realized that up to thirty-nine is a single pattern ( ^(([X]{0,3}([I]([XV]?|[I]{0,2})?|([V][I]{0,3})?)))$ , changed to capturing groups for better clarity). 认识到多达三十九是单一模式( ^(([X]{0,3}([I]([XV]?|[I]{0,2})?|([V][I]{0,3})?)))$ ,改为捕捉组以更清晰)。
  6. Changed pattern to allow up to twenty-nine. 更改模式允许最多二十九个。
  7. Added another to allow thirty to thirty-nine. 添加另一个允许三十到三十九。
  8. Construct the whole pattern and test in RegexBuddy (an invaluable tool for this stuff) against digits 0 - 20,000 and Roman Numerals 1 - 150 followed by "-AA". 构建整个模式并在RegexBuddy(这个东西的宝贵工具)中对数字0 - 20,000和罗马数字1 - 150后跟“-AA”进行测试。
  9. The pattern worked, so I posted it (then grabbed another cup o' coffee and self-administered an 'atta-boy' for completing what I thought was a lovely Saturday morning challenge). 这种模式很有效,所以我发布了它(然后抓起另一杯咖啡,并自我管理一个'男孩'来完成我认为是一个可爱的星期六早上的挑战)。

By extraneous brackets, I assume you mean the non-capturing groups (?: ... ) . 通过无关的括号,我假设你的意思是非捕获组(?: ... ) I use those a lot to group things (and the grouping is quite necessary here). 我使用那些来分组的东西(这里的分组非常必要)。 I made them non-capturing because I do not need to capture the sub-groups, only the parent groups (and in this use case I don't think they need to actually be captured either, but it doesn't hurt to do so). 我让他们不捕获因为我不需要捕获子组,只需要捕获父组(在这个用例中我不认为他们也需要实际捕获,但这样做并没有伤害)。 By making them non-capturing, they won't create backreferences which speeds up processing (though for a single input, the time gained is negligible). 通过使它们不被捕获,它们不会创建加速处理的反向引用(尽管对于单个输入,所获得的时间可以忽略不计)。

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

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