Regular expressions: Matching if number is greater then…?

前端 未结 4 1432
灰色年华
灰色年华 2021-01-16 03:03

I am in the need of an regexp that checks if one numerical value is greater then another value. Pretty much like I would by writing (if 10>8) ...

Cl

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 03:39

    It is not something straightforwardly done with regular expressions. The language you are programming in may allow extensions to the regexp syntax to plug in arbitrary code. For example in Perl:

    $_ = '54 55';
    say 'matched' if /\A(\d\d) (\d\d)(?(?{$1 >= $2})(*FAIL))\z/;
    

    This succeeds but 56 55 does not. In https://stackoverflow.com/a/30936388/626804 I explain a bit further.

    Whether this is good taste or a good idea is another matter - but in Perl you can jam this kind of comparison into a so-called 'regular expression' match if you really want.

提交回复
热议问题