贪婪的正则表达式匹配

发布于 2024-09-16 19:09:21 字数 353 浏览 11 评论 0 原文

我正在尝试匹配看起来像这样的字符串:

<$Fexample text in here>>

使用以下表达式:

<\$F(.+?)>{2}

但是,在某些情况下,我的反向引用内容包含“>”,因此类似这样:

<$Fexample text in here <em>>>

仅匹配此处的 示例文本 < ;em 在反向引用中。我需要做什么才能有条件地返回带有或不带有这些 html 实体的正确反向引用?

I'm trying to match a string that looks something like this:

<$Fexample text in here>>

with this expression:

<\$F(.+?)>{2}

However, there are some cases where my backreferenced content includes a ">", thus something like this:

<$Fexample text in here <em>>>

only matches example text in here <em in the backreference. What do I need to do to conditionally return a correct backrefernce with or without these html entities?

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

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

发布评论

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

评论(3

木落 2024-09-23 19:09:22

您可以将开始和结束锚点添加到正则表达式中,如下所示:

^<\$F(.+?)>{2}$

You can add start and end anchors to the regex as:

^<\$F(.+?)>{2}$
剑心龙吟 2024-09-23 19:09:22

尝试

<\$F(.+?)>>(?!>)

(?!>) 仅强制 >>>..>> 长序列中的最后一个 >> > 将被匹配。


编辑:

<\$F(.+?>*)>>

也有效。

Try

<\$F(.+?)>>(?!>)

The (?!>) forces only the last >> in a long sequence of >>>..>>> will be matched.


Edit:

<\$F(.+?>*)>>

Also works.

一抹苦笑 2024-09-23 19:09:22

请注意,如果你真正做你想做的事情,你就必须解释格式良好的括号表达式,这在常规语言中是不可能的。

换句话说,<$Fexample>>>示例>>哦,这不应该发生> 将返回 example >>>示例>>哦,作为捕获组,这不应该发生

Please note than tu truly do what (I think) you want to do, you would have to interpret well-formed bracket expressions, which is not possible in a regular language.

In other words, <$Fexample <tag <tag <tag>>> example>> oh this should not happen> will return example <tag <tag <tag>>> example>> oh this should not happen as the capture group.

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