我需要一个正则表达式来匹配以#开头的单词。
我写了这样一个问题:如何在php中的正则表达式中匹配磅(#)符号(用于标签),但我忘记解释我需要在单词开头使用#。
我需要匹配#word、#123和#12_sdas,但不能匹配1#234或#1234。
例如,在"#match1 notMatch not#Match #match2 notMatch #match3"
中,只应出现#match1、#match2和#1match3。
编辑:我只想要一磅(#),然后是一个或多个[a-ZA-Z0-9_]。这场比赛以前不可能有任何[a-ZA-Z0-9_]。
我的问题是在单词开头找英镑。
preg_match_all('/(?:^|'s)(#'w+)/', $string, $results);
EDIT:没有php-cli来测试这一点,但regex至少可以在python中工作。
试试这个:
preg_match_all('/(?:^|'s+)(#'w+)/', $your_input, $your_results);
// print results
print_r($your_results);
它将匹配以#
符号开头的所有单词。单词可以由所有有效的空白字符分隔(因此是't'r'n'v'f
中的一个)
示例
//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
)
)