Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 引号之间的哈希值_Regex_Preg Replace - Fatal编程技术网

Regex 引号之间的哈希值

Regex 引号之间的哈希值,regex,preg-replace,Regex,Preg Replace,我正在编写这个小代码。我设法使它在一边使用双引号,但在另一边不使用双引号: /(?不应转换为hashtag。如何将此函数添加到上述代码?此表达式似乎有效: (?<!\S)(?<!".)#([0-9\p{L}]+)+(?=[\s,!?.\n][^"]|$) (? 我猜您可能想设计一个类似于以下内容的表达式: (?<!"\s)#([0-9\p{L}]+)(?=[\s,!?.\n][^"]|$) (? 如果您希望探索/简化/修改该表达式,将在的右上面板中对其进

我正在编写这个小代码。我设法使它在一边使用双引号,但在另一边不使用双引号:


/(?不应转换为hashtag。如何将此函数添加到上述代码?

此表达式似乎有效:

(?<!\S)(?<!".)#([0-9\p{L}]+)+(?=[\s,!?.\n][^"]|$)
(?

我猜您可能想设计一个类似于以下内容的表达式:

(?<!"\s)#([0-9\p{L}]+)(?=[\s,!?.\n][^"]|$)
(?
如果您希望探索/简化/修改该表达式,将在的右上面板中对其进行解释。在中,如果您愿意,可以查看它与一些示例输入的匹配情况

试验
$re='/(?-works)
#行动”-有效
“#行动-不起作用
';
预匹配全部($re,$str,$matches,预设置顺序,0);
var_dump($matches);

当前模式的问题是,您需要一个可变宽度的查找来正确检查每个标签前是否有双引号。我采用的方法是使用
preg\u match\u all
模式,该模式只需要消耗足够的信息来决定是否哈希标记是匹配还是不匹配。请考虑下面的脚本:

preg_match_all('/(?:^|[^"]\s+)(#[0-9\p{L}]+)[.;,!?]?(?=$|\s+[^"])/', $input, $matches);
print_r($matches[1]);

 Array
(
    [0] => #action
    [1] => #Action
    [2] => #cool
    [3] => #000000
    [4] => #ffffff
)
以下是对该模式的解释:

(?:^|[^"]\s+)   match the start of the input, OR
                a single non quote character, followed by one or more whitespaces
(#[0-9\p{L}]+)  then match and capture a hashtag
[.;,!?]?        followed by an optional punctuation character
(?=$|\s+[^"])   finally lookahead and assert either the end of the input, OR
                one or more whitespaces followed by a single non quote character

请注意,虽然我们确实匹配了一些我们并不真正想要的内容,但这并不重要,因为第一个捕获组只包含hashtag。

您是字符串html代码吗?您是否试图避免html属性?您是否希望排除双引号之间或双引号之外的匹配磅。任何一种方法都需要平衡的方法最好使用
\G
构造(在您的情况下)或匹配所有引号内容的回调,然后将在该字符串中找到的任何磅放入数组。任何一种方法都可以。它都不能跳过颜色,即#ffffff;我们如何知道什么是和什么不是哈希标记?很简单:
\something
是哈希标记。
“something”
“#某物”
不是。
#某物;
不是。我们的想法是它不应该弄乱HTML代码。但是,当双引号碰到标签时,它会停止工作。
(?:^|[^"]\s+)   match the start of the input, OR
                a single non quote character, followed by one or more whitespaces
(#[0-9\p{L}]+)  then match and capture a hashtag
[.;,!?]?        followed by an optional punctuation character
(?=$|\s+[^"])   finally lookahead and assert either the end of the input, OR
                one or more whitespaces followed by a single non quote character