我想在带有re.findall
的字符串中找到以单个非字母数字字符开始的单词,比如'$'
$Python
$foo
$any_word123
$$Python
foo
foo$bar
\b
不起作用如果第一个字符是字母数字,我可以这样做。你知道吗
re.findall(r'\bA\w+', s)
但这对\b\$\w+
这样的模式不起作用,因为\b
只匹配\w
和\W
之间的空字符串。你知道吗
# The line below matches only the last '$baz' which is the one that should not be matched
re.findall(r'\b\$\w+', '$foo $bar x$baz').
上面的输出['$baz']
,但是所需的模式应该输出['$foo', '$bar']
。你知道吗
我尝试用模式^|\s
替换\b
,但是这不起作用,因为lookarounds的长度必须是固定的。你知道吗
处理这种模式的正确方法是什么?你知道吗
以下内容将匹配以单个非字母数字字符开头的单词。你知道吗
或者只是:
结果:
一种方法是对非空白元字符
\S
使用负lookback。你知道吗相关问题 更多 >
编程相关推荐