Kasper Hansen
Kasper Hansen

Reputation: 6547

How do I search and replace with a multiline regular expression?

I need to edit a large EDI message, which basically is a textfile of thousands of short lines. The reason is that it must comply to the standard specification and doesn't because in some of the segments there are an extra QTY+220 line that must be removed. It is in those segments that has 4 QTY lines where QTY+220 must be deleted. Here is a correct segment:

SEQ++79'
MOA+9:1.87945:NOK'
QTY+58:0'
QTY+136:5'
QTY+260:5'

Here is an incorrect segment:

SEQ++365'
MOA+9:1.31896:NOK'
QTY+58:0'
QTY+136:4'
QTY+220:0'  <---- this line must be removed
QTY+260:4'

The complete textfile is about 75.000 lines and there are more than 2200 of these validation errors in the xml schema. I tried to make a seach and replace with notepad++ and regular expressions, but I can't make it match over multiple lines. Here is a single line:

^QTY.*'

But I want it to find matches of 4 QTY-lines and remove the 3rd line. How can I do that?

Upvotes: 0

Views: 50

Answers (1)

Cephalopod
Cephalopod

Reputation: 15145

Use \n to match linebreaks.

In your example, replace

(QTY[^\n]+)\n(QTY[^\n]+)\n(QTY[^\n]+)\n(QTY[^\n]+)

with

$1\n$2\n$4

to remove the third line

Upvotes: 3

Related Questions