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

add a zero after regex target lines

I have a list like following:

Xbo
Play
Sep2018
/////////////////////////
reregg
95959
1151
Zbo
Tlay
Jul2018

Now I want to add a zero between lines before date lines that start with a character and consecutive like following:

Xbo
0
Play
0
Sep2018
/////////////////////////
reregg
95959
1151
Zbo
0
Tlay
0
Jul2018

I tried following regex but not working:

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

find : (?m)(?:^[A-Za-z].*\R([A-Za-z].*)$\R\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b)|(?<!\A)\G[A-Za-z].*\R)\K
replace : 0\n

where is my regex problem?

>Solution :

You may use this regex for search:

(?:^[A-Za-z].*\R\K|(?!^)\G)(?=(?:[A-Za-z].*\R)*(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b)

and replace with:

0\n

RegEx Demo

RegEx Details:

  • (?:: Start non-capture group
    • ^[A-Za-z].*\R\K: Start with a line that starts with a letter till end of line and then reset the match
    • |: OR
    • (?!^)\G: Start matching from the end of previous match
  • ): End non-capture group
  • (?=: Start positive lookahead
    • (?:[A-Za-z].*\R)*: that matches 0 or more lines starting with a letter
    • (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b: Followed by Month and 4 digit year
  • ): End positive lookahead
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