正则表达式 - 引用搜索中的第一个匹配项

发布于 2024-11-04 20:59:59 字数 627 浏览 0 评论 0原文

我不太知道如何用简短的标题来描述我的问题,所以如果这个问题的标题有点误导,我很抱歉。 但我真的不知道我正在寻找的东西叫什么,或者是否可能。

我正在尝试使用正则表达式来查找 HTML 中一组匹配标签之间的所有内容。 当我使用静态标签进行测试时,这对我来说很容易,因为我可以搜索两段文本之间的所有内容,例如 \{myTag\}(someExpression)\{\/myTag\}

My问题在于“myTag”可以是任何东西。 我只是不知道当文本可变时如何(或者是否可能)将开始标签与结束标签匹配。

我想我之前在正则表达式中见过某种引用系统,您可以在其中使用美元符号和数字,但我不知道您是否可以在搜索本身中使用它。

我原本以为也许我可以写这样的东西: \{(.*?)\}(someExpression)\{\/${1}\} 但我不知道这是否真的有效或者是否可能(更不用说它是否正确)。

我希望这个问题有意义,因为我不太确定如何问。 主要是因为就像我说的,我不知道这是否有名字,是否可能,而且我也是常规表达的初学者。

如果有什么区别的话,我正在使用 PHP 语言来实现此目的,并使用 preg_replace_callback 函数。

任何帮助将不胜感激。

I don't quite know how to describe my problem in a short title so I am sorry if the title for this question is a bit mis-leading.
But I really don't know what the thing I am looking for is called or if it is even possible.

I am trying to use a regular expression to find everything between a set of matching tags in HTML.
This was easy for me when I was testing with static tags because I could just search for everything in between two pieces of text such as \{myTag\}(someExpression)\{\/myTag\}

My problem comes with the fact that 'myTag' could be anything.
I just don't know how (or if it is even possible) to match the starting tag with the ending tag when that text is variable.

I thought I had seen some kind of referencing system in regular expressions before where you can use the dollar sign and a number, but I don't know if you can use this within the search itself.

I originally thought that perhaps I could write something like: \{(.*?)\}(someExpression)\{\/${1}\} but I have no idea if this would actually work or if it is possible (let alone if it is correct).

I hope this question makes sense as I'm not really sure how to ask it.
Mainly because like I said I don't know if this has a name, if it is possible and I am also a total beginner at regular experessions.

And if it makes any difference the language I am doing this is in PHP with the preg_replace_callback function.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

肤浅与狂妄 2024-11-11 20:59:59

试试这个:

\{([^}]*)\}(someExpression)\{\/\1\}

但请注意,您需要确保 someExpression 也不匹配结束标记(例如 .* 会)。当然,如果标签是嵌套的,那么所有的赌注都会被取消,并且您将需要不同的正则表达式(或解析器)。

Try this:

\{([^}]*)\}(someExpression)\{\/\1\}

but be aware that you need to make sure someExpression doesn't match ending tags as well (like for example .* would). And of course, if tags are nested, then all bets are off, and you'll need a different regex (or a parser).

扭转时空 2024-11-11 20:59:59

这取决于你的情况。如果您知道它只是一个 HTML 片段,并且有一个可以搜索 HTML 的特定模式,那么您可以使用正则表达式来查找和替换该模式,但在我看来,您正在尝试解析 HTML。所以问题是如果你有一个嵌套标签。您应该查看 http://php.net/manual/en/function.preg -replace.php 因为这似乎是一个比带有回调的函数更容易使用的函数。

作为有关正则表达式回顾的注释,您可以根据您使用的语言使用 $i 或 \i。我不知道 php 正则表达式是否支持捕获组回溯。

It kind of depends on your case. If you know it's just an HTML snippet and there is a specific pattern you can search the HTML for then you can use a regex to find and replace the pattern but it seems to me you are trying to parse the HTML. So the issue would be if you had a nested tag. You should check out http://php.net/manual/en/function.preg-replace.php because that seems like a much easier function to use than the one with the callback.

As a note about regular expression look backs you can use $i or \i depending on the language you are using. I don't know if php regex supports capturing group look backs.

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