Home > Enterprise >  RegEx to select only the white spaces before a certain word
RegEx to select only the white spaces before a certain word

Time:11-27

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 , 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 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

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 ' ' whenever there's a space in the path before "zip" and everywhere when the path doesn't have any zip.

CodePudding user response:

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.

  • Related