Make unwanted RegEx in the opposite direction to behave the same as in the forward direction

This template:

/a+?b+?/

Against the following line:

aaaaaabbbbbb

Matches:

aaaaaab

We see that non-living behave differently in the opposite / left direction (accepts everything) and in the forward / right direction (takes only one).

Is there a way to make non-greedy at the beginning that matches everyone ato match as little as possible? So that he behaves the same as with the part bat the end?

+5
source share
3 answers

, ( ). , , lookbehind.

- , , ? ?

() , , . , , , , .

, :

_abc_END_def_END

:

(\w+END)(\w+END)?

, , _abc_, END, _def_, END. , -, , .

, END \w+, "" \w+, _abc_END_def_, END. .

. END \w+ , END .

- "" - , , , .

b a, . a, , b, , :

ab

, a stand-in , b:

[ab]b

:

\wb

:

ab
+2

! ( +)

  • ,
  • ( +)
  • , .

"" "", , , .

? , - ( ab, , , , ).

+1

If you don’t need to perform the previous match from right to left, you can simply change the line, cancel the regex expression, and then cancel the result at the end.

The work is as follows:

Start with aaaaaabbbbbb
Reverse to bbbbbbaaaaaa
Reverse /a+?b+?/ to /b+?a+?/
The resulting Match is bbbbbba
Reverse the resulting match to get abbbbbb
+1
source

All Articles