.NET Regex 类的运算符优先级记录在哪里?

发布于 2025-01-07 19:20:49 字数 476 浏览 0 评论 0原文

.NET 正则表达式记录的运算符优先级顺序在哪里 类?

我看到这个,但那是为了JScript。

此外,这似乎不受 RegexOptions.ECMAScript,但确认会有所帮助。

Where is the order of operator precedence documented for the .NET Regex class?

I see this, but that's for JScript.

Also, it appears this is not affected by RegexOptions.ECMAScript, but confirmation would be helpful.

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

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

发布评论

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

评论(3

仄言 2025-01-14 19:20:49

这个问题很久以前就有人问过,但没有得到解答。

.Net 正则表达式运算符优先级在正则表达式语法页面底部定义 1

This was asked a long time ago, but not answered.

The .Net regex operator precidence is defined at the bottom of the Regular Expression Syntax page 1.

握住我的手 2025-01-14 19:20:49

我认为真正的答案并不像您想要的那么简单。简短(且不完整)的答案只是“所有表达式都是从左到右计算的”......

对于长答案,请转到此处。

http://msdn.microsoft.com/en-us/library/e347654k.aspx

.NET Framework 正则表达式引擎是回溯正则
表达式匹配器结合了传统的非确定性
有限自动机 (NFA) 引擎...传统 NFA 引擎的性能
模式匹配,它们的处理顺序由常规驱动
表达模式。当它处理特定的语言元素时,
引擎使用贪婪匹配;也就是说,它匹配尽可能多的
尽可能输入字符串。但它也会在之后保存其状态
成功匹配子表达式。如果比赛最终失败,
引擎可以返回到已保存的状态,以便可以尝试其他
匹配。


编辑:忘记回答问题的第二部分。

关于RegexOptions.ECMAScript,您可以参考这些文档:

http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx

ECMAScript 和规范正则表达式的行为不同
三个方面:字符类语法、自引用捕获
组,以及八进制与反向引用解释。

看起来“订单”没有受到影响,但可能还有其他问题。这些文档很好地说明了差异。

I don't think the real answer is as simple as you'd like. The short (and incomplete) answer is simply "all expressions are evaluated left to right" ...

For the long answer go here.

http://msdn.microsoft.com/en-us/library/e347654k.aspx

The .NET Framework regular expression engine is a backtracking regular
expression matcher that incorporates a traditional Nondeterministic
Finite Automaton (NFA) engine ... traditional NFA engines perform
pattern matching, their processing order is driven by the regular
expression pattern
. As it processes a particular language element,
the engine uses greedy matching; that is, it matches as much of the
input string as it possibly can. But it also saves its state after
successfully matching a subexpression. If a match eventually fails,
the engine can return to a saved state so it can try additional
matches.

Edit: Forgot to answer the second part of your question.

With regard to RegexOptions.ECMAScript, you can reference these docs:

http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx

The behavior of ECMAScript and canonical regular expressions differs
in three areas: character class syntax, self-referencing capturing
groups, and octal versus backreference interpretation.

It doesn't look like "order" is affected, but there could be other issues. The docs do a good job of illustrating the differences.

风情万种。 2025-01-14 19:20:49

为了澄清 Donald Rich 的回答,Microsoft 尚未提供 .NET Regex 引擎的记录优先级顺序。不过,他们确实记录了旧 Visual Studio 2010 JScript 引擎的优先顺序,这似乎与 .NET 的行为方式相符。我在下面复制了它,以防 Microsoft 文档消失。

来自 正则表达式语法 >优先顺序

正则表达式的计算方式与算术表达式非常相似;也就是说,它是从左到右计算的,并遵循优先顺序。

下表包含正则表达式运算符的优先级顺序(从最高到最低)。

<表类=“s-表”>
<标题>

一个或多个运算符
描述


<正文>

\
逃脱

()、(?:)、(?=)、[]
圆括号和中括号

*、+、?、{n}、{n,}、{n,m}
量词

^, $, \任何元字符
锚点和序列

|
交替

字符的优先级高于交替运算符,例如,允许“m|food”匹配“m”或“food”。

To clarify Donald Rich's answer, Microsoft hasn't provide a documented order of precedence for the .NET Regex engine. However, they did document the precedence order for the old Visual Studio 2010 JScript engine, which seems to match how .NET behaves. I've duplicated it below in case the Microsoft docs disappear.

From Regular Expression Syntax > Order of Precedence:

A regular expression is evaluated much like an arithmetic expression; that is, it is evaluated from left to right and follows an order of precedence.

The following table contains the order of precedence of the regular expression operators, from highest to lowest.

Operator or operatorsDescription
\Escape
(), (?:), (?=), []Parentheses and brackets
*, +, ?, {n}, {n,}, {n,m}Quantifiers
^, $, \anymetacharacterAnchors and sequences
|Alternation

Characters have higher precedence than the alternation operator, which, for example, allows "m|food" to match "m" or "food".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文