Why does my regex do this?

Consider the following text:

foo:·····¶
·bar x··¶
·lorem ipsum····¶
dolorsitamet···¶
···¶   
consectetur adipiscing elit:

Where midpoints indicate spaces, and denotes a new line.

The regular expression (?:foo:\s*)(.+)(?:\n\s*)matches the three spaces in the white space, but the regular expression (?:foo:\s*)(.+)(?:\n\s+)matches and does not include the new line following dolorsitamet. Why is the symbol * not behaving greedily? One would expect the third group (?:\n\s*)to fit


···¶`

My regex mode is multi-line, dotted.

+4
source share
2 answers

The greedy nature is not a problem here. Problems arise when using:

\n\s*

.+ , , \n\s* \n , .. (last-1)th (.. 3 ) .

- RegEx 1

,

\n\s+

- + \n, \n (last-1)th ( ). , \n , dolorsitamet, .

RegEx Demo 2

+1

. , \n\s+ , , . , .

, .+ , , , .

+1

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


All Articles