Replace the LAST found "\" in every line with "\\"

Find, replace, find in files, replace in files, regular expressions

Replace the LAST found "\" in every line with "\\"

Postby c4p0ne » Sat Oct 04, 2008 1:25 pm

There are multiple "\" characters on each line, what is the proper syntax to replace the LAST "\" character in every line with "\\"?

thanks.
User avatar
c4p0ne
Newbie
 
Posts: 9
Joined: Sat Oct 04, 2008 1:21 pm
Location: Classified

Re: Replace the LAST found "\" in every line with "\\"

Postby pietzcker » Sat Oct 04, 2008 3:40 pm

Perl regex:

Search for \\([^\\]*)$
Replace with \\\\\1
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Replace the LAST found "\" in every line with "\\"

Postby c4p0ne » Sun Oct 05, 2008 7:58 am

Nice thanks, but doesn't seem to be working, it's selecting the "\" and everything after it sort of like:

???? ???\25644d244vcv4d13e8b79eb65bb029c73\}:J\fake@fakeaddress.com

I need to isolate only that last "\" and replace it with "\\" (on every subsiquent line as well).
User avatar
c4p0ne
Newbie
 
Posts: 9
Joined: Sat Oct 04, 2008 1:21 pm
Location: Classified

Re: Replace the LAST found "\" in every line with "\\"

Postby Mofi » Sun Oct 05, 2008 9:10 am

If you would have tried the replace, you would have seen that the replace works. Yes, the search strings also selects everything after last \ till end of line, but the replace does not modify this part. The string after last \ is tagged by ([^\\]*) and re-used in the replace string with expression \1 which means everything found by the expression in the first (...) of the search string.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replace the LAST found "\" in every line with "\\"

Postby pietzcker » Sun Oct 05, 2008 1:51 pm

Thanks for your support, Mofi! :)

Of course, using lookahead, you could also search for

\\(?=[^\\]*$)

and replace all with

\\\\

Then you really only select the last \ on the line. This will even be quite a bit faster than my previous solution, I expect.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Replace the LAST found "\" in every line with "\\"

Postby c4p0ne » Mon Oct 06, 2008 11:04 am

Thanks for your time and patience in this simple matter. It is much appreciated! :)
User avatar
c4p0ne
Newbie
 
Posts: 9
Joined: Sat Oct 04, 2008 1:21 pm
Location: Classified

Re: Replace the LAST found "\" in every line with "\\"

Postby mingwu » Thu Oct 09, 2008 1:55 am

pietzcker's lookahead version is the best.

Cheers,

Ming Wu
mingwu
Newbie
 
Posts: 3
Joined: Fri Aug 22, 2008 6:39 am


Return to Find/Replace/Regular Expressions