-1

I am looking for regex pattern where it will search string only if another string matches in previous few lines, for e.g.

abc----1
pqr----2
123----3
xyz----4
lll----5
pqr----6
123----7
qqq----8

so here say I want to find 123 only if we go above and first find xyz and not abc. So outout should be only matching pattern is line no 7 and not 3. Thanks, Tim for an answer, One more additional criteria are I wanted to replace 123 only not all line found by this pattern

1 Answers1

0

You may try searching on the following pattern:

xyz((?!abc).)*?123

This matches xyz which is then not followed by abc anywhere before encountering 123 later in the text. You should run the above pattern in dot all mode, so that .* can match across newlines.

Demo

Edit:

To replace the 123 with some other content, say 456, you may capture everything leading up the 123, then replace with the captured quantity followed by the new text:

Find:    (xyz(?:(?!abc).)*?)123
Replace: $1456

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I copied this demo example in notepad++ with regex search, but it is not able to find it, basically, I want to replace 123 with some other text but with only matching criteria,i.e. if in the previous line if xyz is there and not abc – lone_player Aug 06 '20 at 16:17
  • `You should run the above pattern in dot all mode` ... did you enable dot all mode as my answer suggests? – Tim Biegeleisen Aug 06 '20 at 16:17
  • Thanks Tim, it worked but I think I missed to provide additional detail where I just wanted to replace only 123 , but with this search pattern it will find all lines but I cant replace only 123 – lone_player Aug 06 '20 at 16:23
  • Check the updated answer for one way to do this in NPP. – Tim Biegeleisen Aug 06 '20 at 16:27