Search code examples
regexnotepad++

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:

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