Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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
如何在PHP中使用正则表达式匹配单词开头的pound()符号_Php_Regex_Hashtag - Fatal编程技术网

如何在PHP中使用正则表达式匹配单词开头的pound()符号

如何在PHP中使用正则表达式匹配单词开头的pound()符号,php,regex,hashtag,Php,Regex,Hashtag,我需要一个正则表达式来匹配以开头的单词 我写了这个问题,但我忘了在一个词的开头解释我需要这个词 我需要匹配word、123、12sdas,但不是1234或1234 例如,在match1 notMatch notMatch match2 notMatch match3中,只应显示match1、match2和match3 编辑:我只想要一磅,后面跟着一个或多个[a-ZA-Z0-9]。比赛之前不能有[a-ZA-Z0-9]中的任何一个 我的问题是在单词的开头找到英镑 preg_match_all('/(

我需要一个正则表达式来匹配以开头的单词

我写了这个问题,但我忘了在一个词的开头解释我需要这个词

我需要匹配word、123、12sdas,但不是1234或1234

例如,在match1 notMatch notMatch match2 notMatch match3中,只应显示match1、match2和match3

编辑:我只想要一磅,后面跟着一个或多个[a-ZA-Z0-9]。比赛之前不能有[a-ZA-Z0-9]中的任何一个

我的问题是在单词的开头找到英镑

preg_match_all('/(?:^|\s)(#\w+)/', $string, $results);
编辑:没有php cli来测试这一点,但正则表达式至少可以在python中工作。

尝试以下方法:

preg_match_all('/(?:^|\s+)(#\w+)/', $your_input, $your_results);
// print results
print_r($your_results);
它将匹配以符号开头的所有单词。单词可以由所有有效的空白字符分隔,因此\t\r\n\v\f

范例


不确定php语法,但正则表达式应该是这样的,对第一个字母使用单词边界和不区分大小写的检查


/\b[a-z]\w+\b/i

请参见右侧的相关帖子列表。我认为您需要更清楚地解释应该匹配哪些内容。不清楚在哪些情况下允许使用数字。e、 g.为什么匹配123而不是1234这不需要hashsymbol位于单词的最开头…现在需要!对不起,错过了那个要求。+1在我意识到它不应该匹配1234之前,我不知道为什么它应该匹配123而不是1234…+1这个,接受了这个。如果它位于非\w字符之后,则可以匹配1234。正则表达式很好,但是上面的正则表达式将hashtag与$match分开,并分别在位置0和1返回标记和标记。正则表达式开头的“^”需要空间,并且只在开头的标记上起作用。。我想您需要/?:^|\s+\w+/'不工作。它没有检测到任何东西,修正了正则表达式在行开始时匹配的问题。并添加了示例输出。如果要匹配abc的abc部分,请删除第二个\b$DEF在php上不起作用,我已经试过了。
//input
#match1 notMatch not#Match #match2 notMatch #match3
//output
Array
(
    [0] => Array
        (
            [0] => #match1
            [1] =>  #match2
            [2] =>  #match3
        )

    [1] => Array
        (
            [0] => #match1
            [1] => #match2
            [2] => #match3
        )

)