Home > Blockchain >  How to replace all lines except one word per line using a regular expression
How to replace all lines except one word per line using a regular expression

Time:08-06

How do you replace all lines except one word per line using a regular expression?

I tried the following regex that mostly works but all the lines after the last match remain.

Find what: (?s)(.*?property": ")(\w )(.*?$)

Replace with: \2\n

Simplified text file:

random lines of text
{
    "property": "my1Value",
    "property": "my2Value",
    "property": "my3Value",
}
more random lines of text

Expecting one word per line:

my1Value
my2Value
my3Value

CodePudding user response:

You could just match single characters when the main pattern doesn't match. These will then get replaced by the empty string (since the capture groups captured nothing):

Find: (?s)"property": "([^"] )".*?(\R)|.

Replace: \1\2

  • Related