Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove specific lines of a regex pattern targets

I have a list like following:

ABC
5
0
gdfgdfg
-----------
GHN
3
0
ffffffff
fffff
ffff
trte
-------------
H
1
0
----------------

I would like to delete lines where the line above them starts with a character, and the line below them starts and ends with a zero.
for example above list must convert to following:

ABC

0
gdfgdfg
-----------
GHN

0
ffffffff
fffff
ffff
trte
-------------
H

0
----------------

following regex can select target lines but I don’t know how to remove second lines of regex:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

^(.+\S)$\R^(\d+)$\R^(0)$

note that second lines must include only a single number between 0-9

>Solution :

You can use

^(.*\S\R)(\d)(\R0)$

Replace with $1$3. See the regex demo.

It is the same as find ^(.*\S\R)\d(\R0)$ and replace with $1$2 but I kept the third group just in case you need it.

Details

  • ^ – start of a line
  • (.*\S\R) – Group 1: a line ending with a non-whitespace char and a line break (sequence)
  • (\d) – Group 2: a digit between 0 and 9
  • (\R0) – Group 3: a line break (sequence) and 0
  • $ – end of a line.

See the demo screenshot:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading