暂无 | 14487

特殊序列

附加的特殊序列是 \A, \Z,\B。 序列\A和\Z分别匹配字符串的开始和结束。 序列\b匹配\w和\W 之间的空字符串,或\w字符和字符串的开头或结尾。非正式地,它代表单词之间的边界。 序列\B与其他地方的空字符串匹配。

例如:

import re
 
pattern = r"\b(cat)\b"
 
match = re.search(pattern, "The cat sat!")
if match:
   print ("Match 1")
 
match = re.search(pattern, "We s>cat<tered?") if="" match:="" print="" ("match="" 2")="" match="re.search(pattern," "we="" scattered.")="" 3")="" <="" pre="">
 
<p class="resu"><b>结果:<b></b></b></p><b><b>
<pre class="brush:python">
>>>
Match 1
Match 2
>>>
</pre>
 
<div class="tip">
"\b(cat)\b"基本匹配单词“CAT”包围的单词边界。
</div>
 
                    </b></b></tered?")>

0

发表评论

    评价:
    Rich Text Editor
    最新评论