Lazy regex not working properly C #

I have the following regex: a?\W*?b and I have a string ,.! ,b
When searching for a match, I get ,.! ,b, but not just bas I expect. Why is this? How to change regex to get what I need?
Thanks for the help.

+3
source share
7 answers

The len quantifier does not help here what you want. Let's see what happens.

The regex engine starts at the beginning of the line. First tries to match a. He cannot, but this is not a problem, since ait is not mandatory.

\W*?, regex , .

b. , , \W*?. b (- ). . , , , b. - .

, - , . : ?

, :

b , a , a b,

b|a\W*b
+4

, , , .

, , , .

, , , , .

+1

, : (a\W*)?b

, , .

0

:

  • a, (" )
  • , -, , ( ",.!" )

regexp b ( ).

',.!, db ' b.

0

a? : " , a" - , ,

\W* : " ", , ,

b " b", . .

, , - .

0

, a? , b , ,.! ,b, lookbehind (?=\W*?)b.

b, , " ", ( )

say a b , a,.! ,b, : (a?)\W*?(b), a, , 2 b

0

. , , . , , . ,

Regex.Match("foo bar", @"\w+ (?:b|bar)")

... foo b, , , . ( , Perle- , .NET, , awk egrep, . , - , - , .)

, , , .

0

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


All Articles