Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to replace multiple lines that match a pattern

Tags:

regex

If I have a bunch of text like this

The cat sat on the mat
Expropriations for international monetary exchange ( Currenncy: Dollars,
                                                     Value: 50,000)
The cat sat on the mat
Expropriations for international monetary exchange ( Currenncy: Yen)
The cat sat on the mat

Is there a Regular Expression I could use in the Find/Replace feature of my text editor (Jedit) to identify all of the lines that begin with the word Expropriations and end with a closing parenthesis and then put those lines inside square brackets so that they look like this:

The cat sat on the mat
[Expropriations for international monetary exchange ( Currenncy: Dollars,
                                                     Value: 50,000)]
The cat sat on the mat
[Expropriations for international monetary exchange ( Currenncy: Yen)]
The cat sat on the mat

The tricky thing is that the closing parenthesis could fall at the end of the same line as the word 'Expropriations' or at the end of the next line. (there might even be more than one line before the parentheses are closed)

like image 421
Bowe Avatar asked Nov 05 '22 15:11

Bowe


1 Answers

You can match:

^(Expropriations[\d\D]*?\))

and replace it with:

[$1]

\d\D matches any single char including the newline.

like image 66
codaddict Avatar answered Nov 15 '22 04:11

codaddict