<p>我想在带有<code>re.findall</code>的字符串中找到以单个非字母数字字符开始的单词,比如<code>'$'</code></p>
<h3>匹配词示例</h3>
<pre><code>$Python
$foo
$any_word123
</code></pre>
<h3>不匹配单词示例</h3>
<pre><code>$$Python
foo
foo$bar
</code></pre>
<h3>为什么<code>\b</code>不起作用</h3>
<p>如果第一个字符是字母数字,我可以这样做。你知道吗</p>
<pre><code>re.findall(r'\bA\w+', s)
</code></pre>
<p>但这对<code>\b\$\w+</code>这样的模式不起作用,因为<code>\b</code>只匹配<code>\w</code>和<code>\W</code>之间的空字符串。你知道吗</p>
<pre><code># 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').
</code></pre>
<p>上面的输出<code>['$baz']</code>,但是所需的模式应该输出<code>['$foo', '$bar']</code>。你知道吗</p>
<p>我尝试用模式<code>^|\s</code>替换<code>\b</code>,但是这不起作用,因为lookarounds的长度必须是固定的。你知道吗</p>
<p>处理这种模式的正确方法是什么?你知道吗</p>