Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of Unicode alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore Unicode character. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.
Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.
我不认为这样就可以得到下面的
O(n)
(因为您需要至少在字符串中迭代一次)。你可以做一些优化。在我假设您想要匹配“整个单词”,例如查找
foo
应该如下匹配:因此,仅仅基于空间的夹板不会起作用,因为:
^{pr2}$这就是^{} module 派上用场的地方,它将允许您构建引人入胜的条件。例如,regexp中的
\b
表示:因此} :
r'\bfoo\b'
将只匹配整个单词foo
。也不要忘记使用^{现在只需使用^{} 扫描字符串。根据文件:
我假设匹配项是动态生成的,因此它们永远不必一次存储在内存中(这对于大的字符串和许多匹配项很有用)。最后数一数:
您可以使用Counter
即:
^{pr2}$输出:
DEMO
这并没有解决复杂性问题,但简化了代码:
相关问题 更多 >
编程相关推荐