0

I've a bunch of Markdown links with whitespace, and I need to replace the whitespace with %20. So far I've hacked a few solutions, but none that work in VSCode, or do exactly what I'm looking for.

This is the URL format conversion I need:

[My link](../../_resources/my resource.jpg)
[My link](../../_resources/my%20resource.jpg)

\s+(?=[^(\)]*\)) will work on any whitespace inside brackets - but gives false positives as it works on anything with brackets.

(?:\]\(|(?!^)\G)[^]\s]*\K\h+ does the job, but I'm getting some "Invalid Escape Character" messages in VSCode, so I assume the language isn't compatible.

I've been trying to identify the link on the characters ]( but as I'm relatively new to regex, struggling a bit.

I tried with this regex: (?<=\]\()s\+ as this (?<=\]\().+ correctly identifies the url, but it doesn't work.

Where am I going wrong here? Thanks in advance!

EDIT: VSCode find in files doesn't support variable length lookbehind, even though find/replace in the open file does support this. Open to any other solutions before I dive into writing a script!

1
  • You can't do that with a single regex pass in VSCode, so use Notepad++ that has a Boost regex engine and also provides Find/Replace in Files option. The regex is (\G(?!\A)|\[[^][]*]\()([^()\s]*)\s+(?=[^()]*\)) and replace with $1$2%20. Nov 3, 2021 at 22:21
2

VSCode regex does not support \K, \G, or \h, but it does support Lookbehinds with non-fixed width. So, you may use something like the following:

(?<=\]\([^\]\r\n]*)[^\S\r\n]+

Online demo.

2
  • I believe the pattern is self-explanotary. If there's something not clear, please let me know and I'll break it down for you.
    – 41686d6564
    Nov 3, 2021 at 19:34
  • Can you replace [^\S\r\n] with a space character (i.e., if you are not concerned about tabs, vertical tabs and formfeeds)? Nov 3, 2021 at 19:56
1

You can use

(?<=\]\([^\]]*)\s+(?=[^()]*\))

Replace with %20. See the demo screenshot:

enter image description here

Details:

  • (?<=\]\([^\]]*) - a positive lookbehind that matches a location that is immediately preceded with ]( and then any zero or more chars other than ]
  • \s+ - any one or more whitespace chars (other than line break chars in Visual Studio Code, if there is no \n or \r in the regex, \sdoes not match line break chars)
  • (?=[^()]*\)) - a positive lookahead that matches a location that is immediately followed with zero or more chars other than ( and ) and then a ) char.

Since you are using it in Find/Replace in Files, this lookbehind solution won't work.

You can use Notepad++ with

(\G(?!\A)|\[[^][]*]\()([^()\s]*)\s+(?=[^()]*\))

and $1$2%20 replacement pattern. In Notepad++, press CTRL+SHIFT+F and after filling out the necessary fields, hit Replace in Files.

See the sample settings:

enter image description here

9
  • Didn't know that VSCode doesn't match vertical whitespace characters by default. That makes this answer better than mine. Have my upvote :)
    – 41686d6564
    Nov 3, 2021 at 19:47
  • 1
    @41686d6564 More, \[[^\]\[]*\] does not match across lines either, so it also affects negated character classes. This is compliant with Vim, though here the similarity ends (as in Vim, one needs to use \_ to enable linebreak matching while in VSCode, \r or \n must be added anywhere in the pattern (even \n{0} will do though it makes little sense in general). Nov 3, 2021 at 21:15
  • Thanks for both your answers folks - annoyingly, this works for the find-replace in a file, but not in the find in files (ctrl/cmd-shift-f). Looks like they're working on slightly different versions of Regex - the find in files doesn't support variable lookbehinds.
    – jt196
    Nov 3, 2021 at 21:31
  • @jt196 Right, it won't since the regex engine in the Find/Replace in Files uses Rust regex engine. not the ECMAScript 2018 compliant one in in-document search and replace. You will need to use your regex in Notepad++ Replace in Files feature. Nov 3, 2021 at 21:32
  • SO post with some more info.
    – jt196
    Nov 3, 2021 at 21:35
0

In the end, as I'm on a Mac and didn't want to fire up a virtual PC to run Notepad++ (Sublime uses the same engine and Atom doesn't allow you exclude files), I used a combination of a Python script with @Wiktor Stribizew's answer for individual files that weren't picked up by the pattern for whatever reason.

md_url_pattern = r'(\[(.+)\])\(([^\)]+)\)'

def remove_spacing(match_obj):
    if match_obj.group(3) is not None:
        print("Match Object: " + match_obj.group(1) + "(" + re.sub(r"\s+", "%20", match_obj.group(3)) + ")")
        return match_obj.group(1) + "(" + re.sub(r"\s+", "%20", match_obj.group(3)) + ")"

# THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
this_folder = '<my_document_folder>' # fixed folder path
note_path = '<note_folder>' # change this 
full_path = os.path.join(this_folder, note_path)
directory = os.listdir(full_path)
os.chdir(full_path)

for file in directory:
    open_file = open(file, 'r')
    read_file = open_file.read()
    read_file = re.sub(md_url_pattern, remove_spacing, read_file)
    if not read_file:
        print("Empty file!")
    else:
        write_file = open(file,'w')
        write_file.write(read_file)

This script could do with a bit of tidying up and debugging (the odd weird empty file and no subfolder compatibility) but it was the best I could do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.