Find lowest matching string and insert line below

A place to try and solve your RegEx problems.
Post Reply
XJAC
Posts: 3
Joined: Fri Sep 14, 2018 3:28 pm

Find lowest matching string and insert line below

Post by XJAC » Fri Sep 14, 2018 3:41 pm

Hi,

I'm searching for a way/ or how can I build an expression to do something like this.
The file contains numerious lines with a specific string in it. Here I used 'PAR' This string is at the beginning of the line and has other data after it.
Below the last line found with PAR in it, I want to insert a new line with a specific, fixed text

PAR length= 300
PAR width= 600
PAR height= 18
PAR type= 3
Here I want to insert a new line with specific text.

Thanks in advance for everyone who can help me a bit further!
User avatar
DigitalVolcano
Site Admin
Posts: 1079
Joined: Thu Jun 09, 2011 10:04 am

Re: Find lowest matching string and insert line below

Post by DigitalVolcano » Mon Sep 17, 2018 11:11 am

A Regular Expression using Negative Lookaheads should work. This assumes your file has CR/LF line endings (\r\n)

RegEx:

Code: Select all

PAR(?!.*PAR)(.*?)(\r\n)
Replace:

Code: Select all

$0NEW TEXT\r\n
For your example, this produces:

Code: Select all

PAR length= 300
PAR width= 600
PAR height= 18
PAR type= 3
NEW TEXT
XJAC
Posts: 3
Joined: Fri Sep 14, 2018 3:28 pm

Re: Find lowest matching string and insert line below

Post by XJAC » Wed Sep 19, 2018 12:38 pm

Thanks,

This works excellent for what I want to change! :D


Regards!
XJAC
Posts: 3
Joined: Fri Sep 14, 2018 3:28 pm

Re: Find lowest matching string and insert line below

Post by XJAC » Mon Sep 24, 2018 7:57 am

Hi,

I have another question concerning this case.

I have also files where it's like this

PAR ....
PAR ....
PAR ....
D ...

Is it possible when D occurs, the new line is inserted below the D, and if D is not inside the program, it is inserted after PAR?

Thanks!
Post Reply