Why does "a * a +" and "(a {2,3}) * a {2,3}" correspond to "aaaa", but "(a {2,3}) *" cannot?

My understanding *is that it consumes as many characters as possible (eagerly), but "returns if necessary." Therefore, in a*a+, a*returns one (or more?) Character in a+so that it can match.

However, in (a{2,3})*, why does the first “instance” a{2,3}not give a character to the second “instance”, so the second can match?

In addition, the (a{2,3})*a{2,3}first part seems to give the symbol of the second part.

+4
source share
3 answers

aaaa ^(a{2,3})*$.

, :

(a{2,3})* , , .

. .

, . a{2,3} aa aaa. aaaa (a{2,3})*, aaa. a{2,3}, a. , regex- a{2,3}, . aaa , regex .

$ , , . , (a{2,3})*a{2,3} .

+2

:

* , (), ""

. , .

" ". .

, .


  • a*a+ - a, a

  • (a{2,3})*a{2,3} - a, , a (: , , " ", )

  • (a{2,3})* - a ( , a a )

+1

backtracking is only performed if a match is not performed, but aaais a valid match, a negative lookahead (?!a)can be used to prevent matching from matching a.

compare

(aaa?)*

and

(aaa?)*(?!a)
0
source

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


All Articles