How do I put a comma after each word of a specific line using a regex in notepad++?

46 Views Asked by At

I have a line not well formatted in about 1500 files and I plan to correct them in notepad++ with a regex. The thing is that I really suck at regex.

Let me detail my expectations:

Here is the line now:

tags:: tree dog book razor

And here is what I need:

tags:: tree, dog, book, razor

The regex should find the line and add the commas, last word of the line excepted. Some of the pages are well formatted.

I tried many many combinaisons of expressions, some worked in regex101, but nothing worked in notepad++.

2

There are 2 best solutions below

3
Nick On BEST ANSWER

You could use this regex:

(?:(?<=^tags::)|\G(?<!^))(\s*\w+)(?= )

This matches:

  • (?:(?<=^tags::)|\G(?<!^)) : either
    • (?<=^tags::) : positive lookbehind for start-of-string followed by tags::; or
  • \G(?<!^) : end of the previous match (but not at the start of the string)
  • (\s*\S+) : zero or more of spaces followed by one or more non-space characters, captured in group 1
  • (?=\h) : positive lookahead for horizontal whitespace

Replace this with $1,.

Regex demo on regex101

7
Justinas On

Regex Replace /(\w)\s(\w)/ with $1, $2.

Example


\w means any character that is treated as word - basically same as [a-z0-9_]
\s white space character