A regular expression with a named subpattern does not see the best match.

When using a specific subpattern inside reg exp, it does not select the best match, but stops at the first match. Did I forget some flag?

Regular expression: (?<minutes>[0-9]|[1-5][0-9]):(?&minutes); test string: 47:24;.

The expression does not match:

pic 1 (47:24;)

But the line is 47:2;executed correctly:

pic 2 (47: 2;).

If I change the condition "or" to [1-5][0-9]|[0-9], reg exp (?<minutes>[1-5][0-9]|[0-9]):(?&minutes);works just fine. Is there any other way to make the string '47: 24; 'match without state change "or"?

+4
source share
2 answers

PCRE (. ). (?&minutes).

42:24;, 2 of 24 [0-9] ( ), , 4 , ;, (?&minutes) [1-5][0-9]. ( )

: , ( , ). - :

(?<minutes>[1-5]?[0-9]):(?<seconds>[1-5]?[0-9]);

:

(?(DEFINE)(?<sex>[1-5]?[0-9]) for "sexagesimal", not for what you think)
(?<minutes>(?&sex)):(?<seconds>(?&sex));

, , ( ). , , - .

:

  • : [1-5][0-9]|[0-9] .
  • : [1-5][0-9]?|[06-9], [06-9]|[1-5][0-9]? ( )

, PCRE, Perl Ruby.

+2

, . , NFA. PCRE DFA, , PHP.

, , a|b b a, a . b .

\b(?:[1-5][0-9]|[0-9])\b, .

\b[1-5]?[0-9]\b ( stribizhev), . \b - , , , jsut .

+3

Source: https://habr.com/ru/post/1620583/


All Articles