11 月 182020
 

Source: https://www.regextester.com/

Character classes
.any character except newline
\w \d \sword, digit, whitespace
\W \D \Snot word, digit, whitespace
[abc]any of a, b, or c
[^abc]not a, b, or c
[a-g]character between a & g
Anchors
^abc$start / end of the string
\bword boundary
Escaped characters
\. \* \\escaped special characters
\t \n \rtab, linefeed, carriage return
\u00A9unicode escaped ©
Groups & Lookaround
(abc)capture group
\1backreference to group #1
(?:abc)non-capturing group
(?=abc)positive lookahead
(?!abc)negative lookahead
Quantifiers & Alternation
a* a+ a?0 or more, 1 or more, 0 or 1
a{5} a{2,}exactly five, two or more
a{1,3}between one & three
a+? a{2,}?match as few as possible
ab|cdmatch ab or cd
Continue reading »
1 月 232015
 

Source: 比較詳細Python正則表達式操作指南(re使用)_python_腳本之家

Python 自1.5版本起增加了re 模塊,它提供 Perl 風格的正則表達式模式。Python 1.5之前版本則是通過 regex 模塊提供 Emecs 風格的模式。Emacs 風格模式可讀性稍差些,而且功能也不強,因此編寫新代碼時儘量不要再使用 regex 模塊,當然偶爾你還是可能在老代碼裡發現其蹤影。

Continue reading »