Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing all text between delimiters only when there is a line feed

Tags:

.net

regex

I am trying to capture the text between 2 delimiters only when there is also a line feed within the delimiters. So for example if we have the following text.

Organisation Name <<me.company.name>>
ABN/ACN <<me.company.abn>>
Contact Name <<me.name>>
<<me.PhoneNumber

Another line>>
Email <<me.emailAddress>>

I am wanting to only return the <<me.PhoneNumber \n\n 'Another Line>>

the \n could be anywhere - basically only matches that have at least one \n within the << >> and ignore all other << >>

The pattern I have so far is <<(.?\n)*?>> but this captures all << >> (I'm using C#)

here is an example of what I have tried https://regex101.com/r/sb0wCs/1

Thanks so much for your help

like image 225
matvdl Avatar asked Oct 25 '25 02:10

matvdl


1 Answers

You can use

<<((?:(?!<<|>>).)*?\n(?s:.)*?)>>

See the regex demo. Details:

  • << - a << string
  • ((?:(?!<<|>>).)*?\n(?s:.)*?) - Group 1:
    • (?:(?!<<|>>).)*? - any zero or more chars (other than newline chars) that do not start >> or << char sequence, as few as possible
    • \n - a LF char
    • (?s:.)*? - any zero or more chars (including newline chars), as few as possible
  • >> - a >> string
like image 181
Wiktor Stribiżew Avatar answered Oct 26 '25 18:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!