简体   繁体   English

特定字符的元素之间的正则表达式

[英]Regex between elements for specific characters

Say I have the following String:假设我有以下字符串:

&<div>test & test & test</div><&>

How would I go about finding all the & in between the element tags, ie ></ ?我将如何 go 找到元素标签之间的所有 & ,即></ (doesn't have to be specific to divs) (不必特定于 div)

So far I've gotten >.*(&).*< , but that only finds the last & in between the ></ .到目前为止,我已经得到>.*(&).*< ,但这只能找到></之间的最后一个 & 。

For context, I eventually need to use this regex to translate certain special characters to use the appropriate escape character, but only in between the elements.对于上下文,我最终需要使用此正则表达式来翻译某些特殊字符以使用适当的转义字符,但仅限于元素之间。

To match only the & , use a look ahead:要仅匹配& ,请使用前瞻:

&(?=[^<>]*</)

It's not bullet proof, but will hopefully work for your input.它不是防弹的,但希望对您的输入有用。

The problem is that the .* expression is “greedy”.问题是.*表达式是“贪婪的”。 And I can't see the slash after the final < symbol in your regexp.而且我在您的正则表达式中的最后一个<符号后看不到斜杠。

The precise answer depends on the RE dialect you're using (you didn't say anything about it), but in common case, this must work:确切的答案取决于您使用的 RE 方言(您没有对此说任何话),但在通常情况下,这必须有效:

>([^<&>]*(&)[^<&>]*)*</

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM