正規表現のまとめ(Python)
Automate Boring Stuff with Python, Chapter 7: Pattern Matching with Regular Expressions.の内容をメモ。
全般
re
モジュールをimportして使う。re.compile()
でregex objectを作成して, これをsearch()
メソッドやfindall()
メソッドで探す。
>>> import re >>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') >>> mo = phoneNumRegex.search('My phone number is 415-555-4242.') >>> print('Phone number found: ' + mo.group()) Phone number found: 415-555-4242
なお、regexパターンのテストはhttps://pythex.org/でできる。
まとめ
- The
?
matches zero or one of the preceding group. - The
*
matches zero or more of the preceding group. - The
+
matches one or more of the preceding group. - The
{n}
matches exactory n of teh preceding group. - The
{n,}
matches n or more of the preceding group. - The
{,m}
matches 0 to m of the preceding group. - The
{n,m}
matches at least n and at most m of the preceding group.