Replacing all spaces between a string and the newline character
-
I need to replace all spaces, only in the lines that contain the string, only from the string to the newline character.
I couldn’t find a satisfactory expression.
The string is: FILE:///
Every space should be replaced with: %20Is it possible to do this?
Can anyone help me?
many thanks
thanks a lot -
-
Thank you very much, it works perfectly!
-
Hello @kiyn, @alan-kilborn and All,
The @alan-kilborn’s search regex is based on the generic regex below :
Which allows to replace any string with an other one, ONLY IF that string is found between two boundaries which may be located in a same line or between two different lines
To be rigourous, I slightly modified the @alan-kilborn’s formulation in order to match all the possible cases !
So, let’s suppose this INPUT text :
file:///D:\Documentation\Musique\Marilyne\Amy Winehouse - Back To Black.mp3 first Test Second test file:///D:\Documentation\Musique\Marilyne\Jason Marz|I'm Yours.mp3 FILE:///D:\Documentation\Musique\Marilyne\Norah Jones - Toes.MP3. Third test Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions - Still Loving You.mp3 fifth and final test file:///D:\Documentation\Musique\Marilyne\Norah Jones - Wake Me Up.MP3My version is :
-
SEARCH
(?i:FILE:///|(?!\A)\G)(?i:(?!\.mp3).)*?\K\x20 -
REPLACE
%20
And we get, after clicking on the
Replace Allbutton ONCE ONLY, the expected OUTPUT text, below :file:///D:\Documentation\Musique\Marilyne\Amy%20Winehouse%20-%20Back%20To%20Black.mp3 first Test Second test file:///D:\Documentation\Musique\Marilyne\Jason%20Marz|I'm%20Yours.mp3 FILE:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Toes.MP3. Third test Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions%20-%20Still%20Loving%20You.mp3 fifth and final test file:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Wake%20Me%20Up.MP3You’ll note that ONLY the space chars, between
file:///orFILE:///and.mp3or.MP3, are replaced by the%20string
If you don’t use the
\Gsyntax, you may also run this simplified version :-
SEARCH
(?i)FILE:///(?:(?!\.mp3).)+?\K\x20 -
REPLACE
%20
But, unlike with the behaviour of the previous regex, you’ll need, this time, to click several times on the
Replace Allbutton till you get the message :Replace All: 0 occurrences were replaced ...:-)Best Regards,
guy038
-
-
Note that @guy038 introduced the notion that this is somehow related to mp3 files; I see no such restriction introduced by OP.
-
Hi @kiyn, @alan-kilborn and All,
Ah… You right about it, Alan. Sorry, I was concentrating on my example !
So, regarding the general case and to be rigourous, we should use the generic regular expression itself :
-
SEARCH
(?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) -
REPLACE RR
where :
FR =
\x20( a space char )RR =
%20BSR =
FILE:///ESR = The end of any link, beginning with
file:///orFILE:///: for example,\.mp3or\.com|\.usor simply\.\w{2,}
Notes :
-
We can replace the
(?-si:modifiers, at the beginning of the regex, by the insensitive modifier(?i:only -
We can omit the
(?s-i:modifiers, in the middle of the regex, and keep only the non-capturing group(?:(?!ESR).) -
We can omit the
(?-si:modifiers and the non-capturing group, all together, at the end of the regex
So the generic regex can simply be expressed as :
-
SEARCH
(?i:BSR|(?!\A)\G)(?:(?!ESR).)*?\KFR -
REPLACE RR
Thus, the functional regex S/R becomes :
-
SEARCH
(?i:FILE:///|(?!\A)\G)(?:(?!\.\w{2,}).)*?\K\x20 -
REPLACE
%20
And from this INPUT text :
file:///D:\Documentation\Musique\Marilyne\Amy Winehouse - Back To Black.com first Test Second test file:///D:\Documentation\Musique\Marilyne\Jason Marz|I'm Yours.fr FILE:///D:\Documentation\Musique\Marilyne\Norah Jones - Toes.us. Third test Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions - Still Loving You.gouv fifth and final test file:///D:\Documentation\Musique\Marilyne\Norah Jones - Wake Me Up.MP3We get this OUTPUT text :
file:///D:\Documentation\Musique\Marilyne\Amy%20Winehouse%20-%20Back%20To%20Black.com first Test Second test file:///D:\Documentation\Musique\Marilyne\Jason%20Marz|I'm%20Yours.fr FILE:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Toes.us. Third test Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions%20-%20Still%20Loving%20You.gouv fifth and final test file:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Wake%20Me%20Up.MP3BR
guy038
-