Rahul Kunjappa
Rahul Kunjappa

Reputation: 97

RegEx to select only the white spaces before a certain word

I have a long string with lots of HTTP URLs separated by a comma, and I'm trying to create a regular expression to find and replace spaces with %20, but only before "zip" in VSCode. Therefore, spaces after the "zip" should be allowed up to the following URL (before a comma ",").

I've tried using "\s(?=[^zip]*zip)" based on the following question; however, it's not working.

Regex to select the white space before a certain character

Sample Text: http://abcd.com/old file/anotherfile.zip/some contents/sample.txt,http://abcd.com/new file/anotherfile.zip/some more contents/new sample.txt,http://abcd.com/newer file/another file.zip/some more contents/old sample.txt,http://abcd.com/newer file/another file.txt

Expected Output: http://abcd.com/old%20file/anotherfile.zip/some contents/sample.txt,http://abcd.com/new%20file/anotherfile.zip/some more contents/new sample.txt,http://abcd.com/newer%20file/another file.zip/some more contents/old sample.txt,http://abcd.com/newer%20file/another%20file.txt

P.S. The string also contains URLs to files, not inside a zip like the last URL in the above sample. Observe that, with the regular expression, I'm trying to replace the selected space with '%20' whenever there's a space in the path before "zip" and everywhere when the path doesn't have any zip.

Upvotes: 1

Views: 147

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

You can use:

\.zip\b[^,]*(*SKIP)(*F)|\s

Details:

  • \.zip\b[^,]*(*SKIP)(*F) - match .zip followed with a word boundary and then any zero or more non-comma chars and then fail the match and start a new search from the failed position
  • | - or
  • \s - a whitespace

See the regex demo.

Upvotes: 1

Related Questions