There's a pattern that starts the same but is a little different as it ends, one continues on the beginning then I want to keep the line remainder, the other I want to keep right away but before the line ends I want to ignore something, there's also content between either of the 2 which I'd like to ignore.

In the following example I want to make a CSV of the deserts†, with the veggies being the pattern and the lorem ipsum the lines in between. I'd like to do this with Notepad++. So far I have .*?carrot\Rpotato (?:cabbage (.*?)|(.*?) turnip)\R.*? replaced with \1, , but that doesn't seem to work; I also suspect it wouldn't be the most efficient way to get what I want.

Thank you kindly

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
potato cheese cake turnip
Vivamus aliquet nibh semper sem sodales mattis.
In a mauris nec eros pulvinar accumsan.
potato cabbage chocolate muffin
Mauris leo lacus, luctus non libero id, mattis gravida tellus.
Nunc eget purus at sapien varius fermentum.
potato vanilla pudding turnip
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Donec et felis orci.
potato cabbage chocolate-covered peanuts
Cras convallis semper erat, sed semper ante lacinia vitae.
Fusce vitae lacus et erat placerat malesuada. 

Expected result:

cheese cake, chocolate muffin, vanilla pudding, chocolate-covered peanuts

1 Answer 1

  • Ctrl+H
  • Find what: .*?carrot\Rpotato (?|cabbage ((?:(?!turnip).)*)\R?|((?:(?!cabbage).)*) turnip).*\R?|.+\R?
  • Replace with: (?1$1,)
  • TICK Match case
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all


.*?             # 0 or more any character but newline, not greedy
carrot          # literally
\R              # any kind of linebreak
potato          # literally
(?|             # branch reset groups
    cabbage         # literally
    (               # group 1
        # tempered greedy token
        (?:             # non capture group
            (?!turnip)      # negative lookahead, make sure we haven't turnip after
            .               # any character but newline
        )*              # end group, may appear 0 or more times
    )               # end group 1
    \R?             # optional linebreak
  |               # OR
    (               # group 1 (same group number as above because of the directive "branch reset groups")
        # tempered greedy token
        (?:             # non capture group
            (?!cabbage)     # negative lookahead, make sure we haven't cabbage after
            .               # any character but newline
        )*              # end group, may appear 0 or more times
    )               # end group 1
     turnip         # literally
)               # end branch reset groups
    .*\R?           # 0 or more any character followed by optional linebreak
|               # OR
    .+\R?           # 1 or more any character followed by optional linebreak


(?1         # if group 1 exists
    $1,         # print the content of group 1 followed by a comma
)           # end if

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

  • Thanks! I'm not sure why the \R were put as optional because the real case will always have a line break in either of the 2 patterns. If you want to, explain why you used "potato (?|" instead of my "potato (?:". Tiny correction to the replacement you did: (?1$1, )
    – DynV
    Jan 13, 2023 at 14:33
  • @DynV: The optional \R? is a typo, you can use \R but it doesn't matter. More infos about Branch Reset Groups better than I can do!
    – Toto
    Jan 13, 2023 at 15:04
  • I intend to reuse this regex and even though for the current real case there's very little chance the mix of terms of both cases, turnip & cabbage, is very unlikely, it may very well be possible in a future case; the mix in question can be seen in this test case. Could you modify your regex so both terms are only verified at those specific location (beginning for cabbage, end for cabbage) ?
    – DynV
    Jan 16, 2023 at 7:00
  • @DynV: Sorry, I don't well understand your new requierement. You should ask a new question with expected result. You could also play with regex here.
    – Toto
    Jan 16, 2023 at 11:14
  • How is this a new requirement? I mentioned both in the explanation and the example that the terms of the differing parts were at the end of the line for turnip and for cabbage right after the initial pattern (that both cases share) ; so turnip only at EOL and cabbage right after potato.
    – DynV
    Jan 16, 2023 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .