.NET 中的惰性正则表达式匹配。这里出了什么问题?

发布于 2024-09-07 10:01:14 字数 576 浏览 6 评论 0原文

在下面的示例中,我想检索 pMAINp 和 first pMDSp 之间的文本。正则表达式有一个向后看和一个向前看:

string contents = "pMAINp MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end";
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+(?=(pMDS)?)";

我希望的结果是: “ MAP B FlightTest Load ”

但它返回的是: “MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end”

您会注意到我正在尝试在这里进行惰性匹配:(pMDS)?这显然不起作用! 任何对此的帮助将不胜感激。谢谢。 :-)

编辑:哎呀,所寻求的文本已被更正。

这很好用:
字符串 blockMainRegex = @"(?<=pMAINp)[\s\w+]+?(?=pMDS)";

In the following example I would like to retrieve the text between pMAINp and the first pMDSp. The regex has a look-behind and a look-ahead:

string contents = "pMAINp MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end";
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+(?=(pMDS)?)";

The result I was hoping for was:
" MAP B FlightTest Load "

but what it returns is:
"MAP B FlightTest Load pMDSp ZutiCarrier pWingp some pMDSp more pWingp end"

You'll notice that I'm attempting a lazy match here: (pMDS)? which clearly isn't working!
Any help with this would be much appreciated. Thanks. :-)

EDIT: Whoops, the sought text has been corrected.

This works great:
string blockMainRegex = @"(?<=pMAINp)[\s\w+]+?(?=pMDS)";

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

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

发布评论

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

评论(2

暗喜 2024-09-14 10:01:14

您会注意到我在这里尝试进行惰性匹配:(pMDS)?这显然不起作用!

您似乎误解了惰性匹配的工作原理。

您将惰性运算符应用于量词 - *、+、?等等 - 在其他地方,它被解释为“零或一”。

如果您希望正则表达式的一部分匹配尽可能少的字符,请将惰性运算符应用于与正则表达式的该部分关联的量词 - 在这种情况下,您希望像这样使用它:

[\s\w+]+?

You'll notice that I'm attempting a lazy match here: (pMDS)? which clearly isn't working!

You seem to be misunderstanding how lazy-matching works.

You apply the lazy operator to a quantifier - *, +, ? etc. - anywhere else, it's interpreted as "zero-or-one".

If you want one part of the regex to match as few characters as possible, apply the lazy operator to the quantifier associated with that part of the regex - in this case, you want to use it like so:

[\s\w+]+?
失眠症患者 2024-09-14 10:01:14
string blockMainRegex = @"pMAINp(.*?)pMDSp";

第一组会有你想要的东西。例如:

Regex re = new Regex(@"pMAINp(.*?)pMDSp");
string result = re.Match(contents).Groups[1].ToString();
string blockMainRegex = @"pMAINp(.*?)pMDSp";

The first group will have what you want. E.g.:

Regex re = new Regex(@"pMAINp(.*?)pMDSp");
string result = re.Match(contents).Groups[1].ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文