仅当存在换行时捕获分隔符之间的所有文本

发布于 2025-01-20 18:59:56 字数 682 浏览 2 评论 0 原文

仅当分隔符内还有换行符时,我才尝试捕获两个分隔符之间的文本。例如,如果我们有以下文本。

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

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

我只想返回 <>

\n 可以是任何地方 - 基本上只匹配在 << 中至少有一个 \n 的匹配项>>>并忽略所有其他 << >>>

到目前为止我的模式是 <<(.?\n)*?>>>但这捕获了所有 << >>> (我正在使用 C#)

这是我尝试过的示例 https://regex101.com/r/sb0wCs/1

非常感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱着落日 2025-01-27 18:59:56

您可以使用

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

regex demo 详细信息

  • &lt;&lt; - a &lt;&lt;&lt; string
  • ((?:(? ;&gt;)。)*?\ n(?s:。)*?) - 组1:
    • (?:(?! ;&gt; &lt;&lt; char序列,尽可能少
    • \ n - lf char
    • (?s:。)*? - 任何零或更多字符(包括newline chars),尽可能少
  • &gt;&gt; - a &gt;&gt; 字符串

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
躲猫猫 2025-01-27 18:59:56

在您的模式中&lt;&lt;(。 *?可以匹配,直到第一次出现&gt;&gt;

也可以在重复捕获组时,组值将保持最后一次迭代的值在您要捕获的整个部分周围没有量词的组。


如果您的字符串从行的开头开始,则可以使用锚点,并在其中至少匹配一条线,这不是从两者开始的。或&gt;&gt;

^\s*<<(.*(?:\r?\n(?!<<|>>).*)+\r?\n)\s*>>$

说明

  • 的开始
  • ^字符串
  • 捕获组1
    • 。*匹配该行的其余部分
    • (?:\ r?\ n(?!&lt;&lt; |&gt;&gt;)。 >&lt;&lt; &gt;
  • \ r?\ n 匹配newline
  • 关闭组1
  • \ s*&gt;&gt; 匹配可选的前导白板char和&gt;&gt;
  • $ 字符串的结尾

请参见a regex demo

In your pattern <<(.*?\n*)*?>> you have a capture group and all parts are optional including the newline, so the non greedy quantifier *? can match until the first occurrence of >>

Also when repeating a capture group, the group value will hold the value of the last iteration, so instead you can put the capture group without a quantifier around the whole part that you want to capture.


If your strings start at the beginning of the line, you can use anchors and match at least a single line in between that does not start with either << or >>

^\s*<<(.*(?:\r?\n(?!<<|>>).*)+\r?\n)\s*>>$

Explanation

  • ^ Start of string
  • \s*<< Match optional leading whitspace chars and <<
  • ( Capture group 1
    • .* Match the rest of the line
    • (?:\r?\n(?!<<|>>).*)+ Match a newline, and repeat at least 1 line not starting with << or >>
  • \r?\n Match a newline
  • ) Close group 1
  • \s*>> Match optional leading whitspace chars and >>
  • $ End of string

See a regex demo.

烟酒忠诚 2025-01-27 18:59:56

您可以尝试一下:&lt;&lt; [^&gt;]*?\ n [^&gt;]*&gt;&gt;&gt;

在此处进行测试: https://regex101.com/r/vd3ege/2

<<[^>]*?\n[^>]*>>

<<      match literal <<
[^>]*?  match any char that is not > as few as possible
\n      match a newline
[^>]*   match any char that is not > as few as possible
>>      match literal >>
  • 仅在有 \ n 之间才能匹配A &lt;&lt; &gt;&gt;

You can try this: <<[^>]*?\n[^>]*>>

Test regex here: https://regex101.com/r/vD3EgE/2

<<[^>]*?\n[^>]*>>

<<      match literal <<
[^>]*?  match any char that is not > as few as possible
\n      match a newline
[^>]*   match any char that is not > as few as possible
>>      match literal >>
  • This will match a only if there is \n between << and >>.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文