Nefco
Nefco

Reputation: 43

Replace whitespace with comma between certain tags in notepad++

I'm trying to write a regex in notepad++ that takes all whitespaces contained between 2 tags and replace them with comma.

So basically, if I have this input:

<foo>bar bar bar</foo>
<tag>bar bar bar</tag>

And I want to replace whitespaces only inside foo tags to get a resut:

<foo>bar,bar,bar</foo>
<tag>bar bar bar</tag>

Matching everything between foo is fairly straightforward:

(?<=(<foo>))(.*)(?=(<\/foo>))

But searching for \s doesn't work:

(?<=(<foo>))(\s)(?=(<\/foo>))

Upvotes: 2

Views: 1213

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

Since you want to replace the spaces inside tags that has no children, nor the same nested tags, you may use

(?:\G(?!^)|<foo>)[^\s<]*\K\s+

See the regex demo, replace with ,.

Details:

  • (?:\G(?!^)|<foo>) - either the end of the previous successful match or <foo>
  • [^\s<]* - zero or more symbols other than whitespace and <
  • \K - omit the text matched so far
  • \s+ - 1 or more whitespaces.

Upvotes: 4

Related Questions