c# - 正则表达式在同一输入上返回不同的结果

标签 c# regex

我正在检查一些结果并传递来自文档的两个输入,两个输入看起来相同但为什么它们返回不同的输出。 我的正则表达式是

(?<preandconjunct>(?:\b([Ss]ubsection|[Ss]ection|[Aa]rticle) +)(?<conjunct>(?:(?<level>(?:(?:[IVXivx]{1,5}(?![A-Z]))|(?:[A-Z]{1,2}(?![A-Z]))|(?:[0-9]+)))|(?<level>\((?:(?:[IVXivx]{1,5}(?![A-Z]))|(?:[A-Z]{1,2}(?![A-Z]))|(?:(?!in|or|if|of|to|as|at|it|no|an)[a-z]{1,2}(?![a-z]))|(?:[0-9]+))\))|(?<level>[\.-](?:(?:[IVXivx]{1,5}(?![A-Z]))|(?:[A-Z]{1,2}(?![A-Z]))|(?:[0-9]+))))+)(?=$|[ ,;.)]))

两个输入是

a dispute under Section 3.1 (which shall be governed exclusively by Section 3.1) or as set forth in Section 11.3(b), the indemnification provisions of this Article XI and Article XII are the sole and exclusive remedies of the Parties pursuant to this Agreement or in connection with the transactions contemplated hereby. From and after the Closing, to the maximum extent permitted by Law, except with respect to claims based on intentional fraud, a dispute under Section 3.1 (which shall be governed exclusively by Section 3.1),

a dispute under Section 3.1 (which shall be governed exclusively by Section 3.1) or as set forth in Section 11.3(b), the indemnification provisions of this Article XI and Article XII are the sole and exclusive remedies of the Parties pursuant to this Agreement or in connection with the transactions contemplated hereby. From and after the Closing, to the maximum extent permitted by Law, except with respect to claims based on intentional fraud, a dispute under Section 3.1 (which shall be governed exclusively by Section 3.1),

我也在期待

第 3.1 节

第 3.1 节

第 11.3(b) 节

第十一条

第十二条

第 3.1 节

3.1 节

最后一个没有出现在第一个输入中。

最佳答案

last one is not showing up in first input

获得所有角色的一种方法是消耗最多一个锚定角色。例如,以文本 blah blah Section 3.1(由 Section 3.1 管理) 为例。我们有三个 anchor ,Section()。让我们基于这些文字 anchor 创建一个模式。

我现在要评论我的正则表达式模式,顺便说一下,它需要 IgnorePattnerWhiteSpace 才能在正则表达式解析器中正常工作。

(((Sub)?Section)|Article)\s+    # Anchor of Section or Article or Subsection
(?<Number>[^\s]+)               # Number involved
\s+
   \(                           # Anchor of '('
      (?<Conjuct>[^)]+)         # Consume til next anchor
   \)                           # ')' anchor.

通过使用 Not[^ ] 我们可以使用任何不是结尾的古怪字符 ) anchor 。我们的比赛结果是这样的

enter image description here

why are they returning different output.

您需要通过查看所提到的文字 anchor 来降低捕获的复杂性。甚至可以通过首先创建文本的一般标记并在第二次正则表达式传递时从标记中提取特定项目来执行两次正则表达式?

也像我所做的那样评论你的模式并处理它的各个部分,一旦各个项目起作用就将整个模式组合在一起。

关于c# - 正则表达式在同一输入上返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44409692/

相关文章:

java - 如何在jtextbox上实时验证正则表达式?

python /正则表达式 : match letter only or letter followed by number

javascript - 为什么这个正则表达式返回这样的数组

C# EPPlus 数据栏条件格式与纯色填充

c# - 如何在 C# 后构建步骤中创建更多宏

c# - 将 Web 应用程序部署到 IIS 时,图像未加载到中继器内

regex - VBA正则表达式获取仅包含数字和连字符的字符串

regex - 在Python中删除字符串上的任何单个字母

c# - WinForms 中的计时器

c# - 如何显示转换后的字符串