正则表达式匹配12345

时间:2010-11-18 19:08:43

标签: regex pattern-matching

是否有正则表达式匹配一串不断增加的连续数字,例如123,56789等? 我不认为它可以在正则表达式,但值得在这里与人们核实。

4 个答案:

答案 0 :(得分:6)

^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?(?:9$)?$

结果:http://rubular.com/r/JfJJ6ntEQG

答案 1 :(得分:6)

^(1|^)((2|^)((3|^)((4|^)((5|^)((6|^)((7|^)((8|^)((9|^))?)?)?)?)?)?)?)?$

Python演示:

>>> import re
>>> good  = ['1', '12', '123', '23', '3', '4', '45', '456', '56', '6', '7', '78', '789', '89', '9', '123456789']
>>> bad   = ['a', '11', '13', '1345', '2459', '321', '641', '1 2', '222233334444']
>>> tests = good + bad
>>>
>>> regex = '^(1|^)((2|^)((3|^)((4|^)((5|^)((6|^)((7|^)((8|^)((9|^))?)?)?)?)?)?)?)?$'
>>> for test in tests:
...   print '%s: %s' % (re.match(regex, test) and 'Passed' or 'Failed', test)
... 
Passed: 1
Passed: 12
Passed: 123
Passed: 23
Passed: 3
Passed: 4
Passed: 45
Passed: 456
Passed: 56
Passed: 6
Passed: 7
Passed: 78
Passed: 789
Passed: 89
Passed: 9
Passed: 123456789
Failed: a
Failed: 11
Failed: 13
Failed: 1345
Failed: 2459
Failed: 321
Failed: 641
Failed: 1 2
Failed: 222233334444

答案 2 :(得分:4)

  

是否有正则表达式匹配一串不断增加的连续数字,例如123,56789等?

当然,因为所有问题的答案开头,“是否有(Perl)正则表达式匹配......?”总是“为什么,当然还有!“操作性问题总是存在,” 是什么是匹配的Perl正则表达式??“☺

简答

Perl正则表达式就是这个:

m{
  ^ (
      ( \d )
      (?(?= ( \d )) | $)
      (?(?{ ord $3 == 1 + ord $2 }) (?1) | $)
    ) $
}x

如果通过具有两个不同的(?(COND)THEN|ELSE)条件组来工作,则将第1组的递归作为第二个的THEN子句。这就是(?1)的作用。

漂亮,嗯?

递归模式就像这些非常酷非常强大; 这取决于你在良好的服务中使用这种力量,而不是邪恶。 ☺

我在下面给出的程序中使用了一种不那么聪明的形式。我将把另一个留在它开始的地方,这样你就可以在Perl 中看到它有多种方法来做它

完整演示程序

请注意,无论是什么 Unicode数字字符串,包括非ASCII(欢迎来到勇敢的新千年),甚至在Astral Plane中,语言都停留在UCS-2上,这都有效,有时甚至是UTF-16,甚至都无法思考。

此输出:

Yes:       3456
 No:         43
Yes:        567
 No:       1245
 No:        568
 No:        987
Yes:         12
Yes:      12345
 No:         24
 No:      13456
 No:   12354678
Yes:   12345678
 No:       ١٣٠٢
Yes:       ٤٥٦٧
 No:        २१३
Yes:       ४५६७
Yes:         ८९
 No:        ১১২
Yes:       ৩৪৫৬
 No:       ৭৮৯৮
Yes:         ௮௯
 No:         ௮௮
 No:       ๖๗๗๘
Yes:        ๖๗๘
 No:    ༣༤༢༥༧༦༨
 No:      01132
Yes:        234
Yes:         89
Yes:       
 No:       
 No:       
Yes:       
Yes:  
Yes:         
 No:      
Yes:    

由该计划制作:

#!/usr/bin/env perl   
use 5.10.0;
use utf8;
use strict;
use autodie;
use warnings  qw<  FATAL all     >;
use open      qw< :std  :utf8    >;
use charnames qw< :full          >;

# to iterate is human…
my @numbers = (
        3456,
          43,
         567,
        1245,
         568,
         987,
          12,
       12345,
          24,
       13456,
    12354678,
    12345678,
    hard_stuff(),
);   
my $ascending_rx = qr{
   ^ (  # works for *ANY* script!
        ( \p{Decimal_Number} ) 
        (?= $ | (??{ chr(1+ord($2)) }) )
        (?: (?1) | $ ) # …to recurse, divine!
    ) $
}x;

for my $n (@numbers) {
    printf "%s: %10s\n",
        ($n =~ $ascending_rx) ? "Yes" : " No",
        $n;
}

sub hard_stuff {   
    ( "\N{ARABIC-INDIC DIGIT ONE}"
    . "\N{ARABIC-INDIC DIGIT THREE}"
    . "\N{ARABIC-INDIC DIGIT ZERO}"
    . "\N{ARABIC-INDIC DIGIT TWO}"
    ),
    ( "\N{ARABIC-INDIC DIGIT FOUR}"
    . "\N{ARABIC-INDIC DIGIT FIVE}"
    . "\N{ARABIC-INDIC DIGIT SIX}"
    . "\N{ARABIC-INDIC DIGIT SEVEN}"
    ),
    ( "\N{DEVANAGARI DIGIT TWO}"
    . "\N{DEVANAGARI DIGIT ONE}"
    . "\N{DEVANAGARI DIGIT THREE}"
    ),
    ( "\N{DEVANAGARI DIGIT FOUR}"
    . "\N{DEVANAGARI DIGIT FIVE}"
    . "\N{DEVANAGARI DIGIT SIX}"
    . "\N{DEVANAGARI DIGIT SEVEN}"
    ),
    ( "\N{DEVANAGARI DIGIT EIGHT}"
    . "\N{DEVANAGARI DIGIT NINE}"
    ),
    ( "\N{BENGALI DIGIT ONE}"
    . "\N{BENGALI DIGIT ONE}"
    . "\N{BENGALI DIGIT TWO}"
    ),
    ( "\N{BENGALI DIGIT THREE}"
    . "\N{BENGALI DIGIT FOUR}"
    . "\N{BENGALI DIGIT FIVE}"
    . "\N{BENGALI DIGIT SIX}"
    ),
    ( "\N{BENGALI DIGIT SEVEN}"
    . "\N{BENGALI DIGIT EIGHT}"
    . "\N{BENGALI DIGIT NINE}"
    . "\N{BENGALI DIGIT EIGHT}"
    ),
    ( "\N{TAMIL DIGIT EIGHT}"
    . "\N{TAMIL DIGIT NINE}"
    ),
    ( "\N{TAMIL DIGIT EIGHT}"
    . "\N{TAMIL DIGIT EIGHT}"
    ),
    ( "\N{THAI DIGIT SIX}"
    . "\N{THAI DIGIT SEVEN}"
    . "\N{THAI DIGIT SEVEN}"
    . "\N{THAI DIGIT EIGHT}"
    ),
    ( "\N{THAI DIGIT SIX}"
    . "\N{THAI DIGIT SEVEN}"
    . "\N{THAI DIGIT EIGHT}"
    ),
    ( "\N{TIBETAN DIGIT THREE}"
    . "\N{TIBETAN DIGIT FOUR}"
    . "\N{TIBETAN DIGIT TWO}"
    . "\N{TIBETAN DIGIT FIVE}"
    . "\N{TIBETAN DIGIT SEVEN}"
    . "\N{TIBETAN DIGIT SIX}"
    . "\N{TIBETAN DIGIT EIGHT}"
    ),
    ( "\N{FULLWIDTH DIGIT ZERO}"
    . "\N{FULLWIDTH DIGIT ONE}"
    . "\N{FULLWIDTH DIGIT ONE}"
    . "\N{FULLWIDTH DIGIT THREE}"
    . "\N{FULLWIDTH DIGIT TWO}"
    ),
    ( "\N{FULLWIDTH DIGIT TWO}"
    . "\N{FULLWIDTH DIGIT THREE}"
    . "\N{FULLWIDTH DIGIT FOUR}"
    ),
    ( "\N{FULLWIDTH DIGIT EIGHT}"
    . "\N{FULLWIDTH DIGIT NINE}"
    ),
    #############################################
    #   Who's afraid of the astral planes?
    #   Try THIS, all you prisoners of UTF-16!
    #############################################
    ( "\N{MATHEMATICAL BOLD DIGIT TWO}"
    . "\N{MATHEMATICAL BOLD DIGIT THREE}"
    . "\N{MATHEMATICAL BOLD DIGIT FOUR}"
    . "\N{MATHEMATICAL BOLD DIGIT FIVE}"
    ),
    ( "\N{MATHEMATICAL BOLD DIGIT FIVE}"
    . "\N{MATHEMATICAL BOLD DIGIT FOUR}"
    . "\N{MATHEMATICAL BOLD DIGIT THREE}"
    . "\N{MATHEMATICAL BOLD DIGIT TWO}"
    ),
    ( "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT ONE}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT TWO}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT TWO}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT THREE}"
    ),
    ( "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT THREE}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT FOUR}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT FIVE}"
    . "\N{MATHEMATICAL DOUBLE-STRUCK DIGIT SIX}"
    ),
    ( "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT ONE}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT TWO}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT THREE}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT FOUR}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT FIVE}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT SIX}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT SEVEN}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT EIGHT}"
    . "\N{MATHEMATICAL SANS-SERIF BOLD DIGIT NINE}"
    ),
    ( "\N{MATHEMATICAL MONOSPACE DIGIT ZERO}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT ONE}"
    ),
    ( "\N{MATHEMATICAL MONOSPACE DIGIT TWO}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT THREE}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT FOUR}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT SIX}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT FIVE}"
    ),
    ( "\N{MATHEMATICAL MONOSPACE DIGIT THREE}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT FOUR}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT FIVE}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT SIX}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT SEVEN}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT EIGHT}"
    . "\N{MATHEMATICAL MONOSPACE DIGIT NINE}"
    ),
}
PS:有人说在Perl中有多种方法可以做到的原因是为了弥补其中有 no 方式的所有其他语言这样做 - 通常是大多数人。 ☻

答案 3 :(得分:2)

Perl:

my $prev = 0;

while($string =~ s/^\s*(\d+)\s*,\s*(.*)/$2/is) {
    my $new = $1;

    if($new > $prev) { 
        #NUMBERS ARE GETTING LARGER
    } #if 
    else {
        #NUMBER GOT SMALLERS
    }
}