简体   繁体   English

匹配未包含某些字符的字符串

[英]Matching string that isn't wrapped with certain characters

I am trying to make automatic tag detection and convertion into hyperlinks. 我正在尝试自动标记检测并转换为超链接。 Problem is, that it has to be done after the string is run through the following: 问题是,必须在字符串运行后执行以下操作:

htmlspecialchars($string, ENT_QUOTES, "UTF-8");

Now, ie, the ' symbol is turned into ' 现在,即'符号变成' . The tags are in the form of #[a-Z0-9\\-\\_] 标签的形式为#[a-Z0-9\\-\\_]

So, the script considers the encoded special characters as tags because of the #39 part. 因此,脚本将编码的特殊字符视为标记,因为#39部分。

How do I match with preg_match so, that it would not consider # marks preceded with & mark as tags? 我怎样匹配preg_match因此,它不会考虑#前面有标记&标记的标签?

Thank you! 谢谢!

You have to use a lookbehind assertion to check that the string is not preceded by a & : 您必须使用lookbehind断言来检查字符串是否前面没有&

Try with this: 试试这个:

"/(?<!&)#[\w-]+/"

The (?<!&) cause the # to match only if it is not preceded by & . (?<!&)只有在#前面没有&时才会使#匹配。

The \\w part matches [a-zA-Z0-9_] \\w部分匹配[a-zA-Z0-9_]

You may also want to check if the tag is preceded by a whitespace or is a the start of the string: 您可能还想检查标记前面是空格还是字符串的开头:

"/(:?^|\s)#[\w-]+/"

Use a Look Behind assertion 使用Look Behind断言

(?<!a)b matches a "b" that is not preceded by an "a" (?<!a)b匹配一个前面没有“a”的“b”

In your case, that would be 在你的情况下,那将是

(?<!&)#[a-Z0-9\-\_]

Will not match # preceded by & 将不会匹配#之前的&

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

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