Roman Goyenko
Roman Goyenko

Reputation: 7070

Regexp to replace repeating string

I have a pipe delimited file and want to replace all the occurrences of |N.D.| with | | .

I did this:

$line =~ s/\|N.D.\|/\| \|/g;

but if the line has repeating N.D. like this:

12354|this is test|N.D|N.D|some more text|

it will only replace one. How do I fix it to replace all?

Upvotes: 1

Views: 104

Answers (1)

bwoebi
bwoebi

Reputation: 23777

Typically you use a simple positive lookahead for this:

\|N\.D(?=\|) and substitute it with |

$line =~ s/\|N\.D(?=\|)/\| /g;

That way the trailing | is not included in the match and the next match will be able to start there.

Upvotes: 2

Related Questions