Home > Enterprise >  regex to replace specific characters while capturing the rest of the line
regex to replace specific characters while capturing the rest of the line

Time:10-08

Using notepad

I need to replace ", " with , on a line beginning exclusively with genre: and no where else in the document, while maintaining all of the other content in the line. I will be applying the search/replace to an entire folder, so I need to be as precise as I can.

Examples

genre: "drama", "thriller", "mystery", "espionage"
genre: "drama", "sci-fi"

should look like this:

genre: "drama, thriller, mystery, espionage"
genre: "drama, sci-fi"

I'm having a hell of a time figuring out how to do that without capturing an unlimited and unknown number of groups before and after each instance of ", ", while also keeping the first word and colon: genre: . I'm pretty sure I have to capture the entire group between the first and last ", and then replace ", " with just , within that group, but I can't figure out how to do that.

Obviously what I have here isn't going to do the trick.

find what: ^genre: "(.*)", "(.*)", "(.*)", "(.*)"
replace with: genre: "$1, $2, $3, $4"

CodePudding user response:

You can use

Find: (?:\G(?!^)|^genre:\h*").*?\K",\h*"
Replace: ,<SPACE>

Details:

  • (?:\G(?!^)|^genre:\h*") - end of the previous match position or genre:, zero or more horizontal whitespaces and " at the start of string (here, line)
  • .*? - any zero or more chars other than line break chars, as few as possible
  • \K - omit the matched text
  • ",\h*" - consume ",, then zero or more horizontal whitespaces, and then a " (this will be replaced with , space)

See the enter image description here

CodePudding user response:

Try this code then,

Find: (?:genre|\G).*?\K", "
Replace All: , there is a space after ","

enter image description here

  • Related