Page 1 of 1

Why does this work?

Posted: Wed Oct 02, 2013 7:02 am
by terrypin
The source text looks like this:

00_c_People\
00_c_SimpleBlue\
2006_Audio\
2006_AutoDefault\

I want it like this:

People\
SimpleBlue\
Audio\
AutoDefault\

Using

Find (.*)_(.*)
Replace with \2

I expected it to fail for the first two lines, delivering:

c_People\
c_SimpleBlue\
Audio\
AutoDefault\

But to my surprise it gave the correct result.

Is that because it looks for the underscore from right to left, rather than left to right as I'd assumed? Or some other explanation?

--
Terry, East Grinstead, UK

Posted: Wed Oct 02, 2013 10:50 am
by MudGuard
* is greedy. Thus the first (.*) takes up as much as possible without breaking global match.
For the first two lines, the first
(.*) matches 00_c.

To make it non-greedy, you have to use *? instead.

Posted: Wed Oct 02, 2013 1:32 pm
by ak47wong
Moreover, you can accomplish the same result more simply with this:

Find what: .*_
Replace with: [nothing]

Posted: Wed Oct 02, 2013 7:51 pm
by terrypin
Thanks both.

.*_ does indeed do the job very neatly!


--
Terry, East Grinstead, UK