Expression finds always the last match

A swapping-ground for Regular Expression syntax

Expression finds always the last match

Postby ertuncb » Mon Mar 25, 2019 8:13 am

Trying to accomplish a simple task: Find the first appearance (from left) of the string "_19" , replace it with the string "_2019".
Looks very straightforward. But BRU finds always the second "_19".

The original file name:
yyy xxxxxx zzz_190311_192559.dat
Match:(.+)(_19)(.+)
Replace:\1_2019\3

When I use the above parameters, the result is:
yyy xxxxxx zzz_190311_20192559.dat
The expected result was:
yyy xxxxxx zzz_20190311_192559.dat

It looks so simple but I couldn't find the problem. Any help will be greatly appreciated.
ertuncb
 
Posts: 2
Joined: Sun Mar 24, 2019 9:54 pm

Re: Expression finds always the last match

Postby Panchdara » Mon Mar 25, 2019 9:07 am

Try this:

Code: Select all
Match: (.*)_19(.*\_.*)
Replace: \1_2019\2
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: Expression finds always the last match

Postby ertuncb » Mon Mar 25, 2019 3:47 pm

Panchdara wrote:Try this:

Code: Select all
Match: (.*)_19(.*\_.*)
Replace: \1_2019\2


Yes, this works. Thank you. I've spent so much time. I would be very grateful for an explanation.
ertuncb
 
Posts: 2
Joined: Sun Mar 24, 2019 9:54 pm

Re: Expression finds always the last match

Postby therube » Mon Mar 25, 2019 4:20 pm

Odd, that didn't seem to work on my end when I looked earlier?


What I came up with (after a bit of reading, as this stuff always confuses me):

Code: Select all
Match:  (.*?_)(19)(.*)
Replace:  \120\2\3


Non-greed match up to a _ followed by 19.
So that should find .*_19 - as two separate variables (whatever you call it).
So the first part \1, matches .*_
The second is the literal 19.
The third is everything else.

\1 = .*_
\2 = 19
\3 = .*

And with a literal 20 thrown in for good luck.


(Now I haven't tested the above in BRU - yet...)

(I've change the Replace [with space] to, \1 20\2 \3, for readability.)
Old:
Code: Select all
install_flash_player_19_plugin_19.0.0.245 (N).exe
install_flash_player_19_active_x_1900245.exe

New:
Code: Select all
install_flash_player_   2019  _plugin_19.0.0.245 (N).exe
install_flash_player_   2019  _active_x_1900245.exe



Alternatively (& again spaced for readability)...
Code: Select all
Match:  (.*?_)19(.*)
Replace:  \1   2019  \2



(And yes, it works in BRU.)


OK, & ertuncb 's method works too - with the above sample.
What I tried it with earlier, was different, the _19 was not consistent, in that the second instance of _1# was not a 9.
(Not quit sure what it was anymore?)


Non-greedy always messes with me (more generally, regex always mess with me ;-)), & I think what I wasn't particularly aware of was that the non-greedy needs to follow the *, so *?.
(Prior I had tried stuff like (.*_?) & (.*_)?, which are incorrect.)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Expression finds always the last match

Postby Panchdara » Tue Mar 26, 2019 6:40 am

therube has a much better solution to mine. His solution will work on multiple _19, whereas mine only worked if there was 2 or less. The greedy/nongreedy characteristics are confusing... more learning, by myself, is required.

Thanks therube, excellent.
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm


Return to Regular Expressions