22. 特殊字元
特殊字元 描述行為說明 特殊字元 描述行為說明
A 字串開頭 s 分隔字元
[ tnrfv]
b 字母或數字前面或後
面是否為分隔字元
(後面說明)
S 非分隔字元
[^ tnrfv]
B 是否為字母或數字中
的一部份 (後面說明)
w 字母或數字
[a-zA-Z0-9_]
d 數字
[0-9]
W 非字母也不是數字
[^a-zA-Z0-9_]
D 非數字
[^0-9]
Z 字串結尾
33. Group
● 以小括號組合有效字元提供regex engine 比
對。
● 範例:
○ regex: (I am ).*
■ I am Wen.
■ I am whatsoever
print re.match(r'(I am ).*', "I am")
None
print re.match(r'(I am ).*', "I am Wen.").group()
I am Wen.
沒有空白
73. 小結
● Assert parse位置不會移動不是喊假的
● 難道下lookahead assertion都要算前面吃幾
個字元?
○ match()需要
○ search(), sub(), findall()不用
■ re.search(pattern, string, flags=0)
● Scan through string looking for the first location where the
regular expression pattern produces a match
● 窮舉檢視字串所有字元,直到找到第一個符合Regex的結果為
止。
https://docs.python.org/2/library/re.html