re【正規表現】3.1, 3.3, 3.4, 3.6, 3.7, 3.8
検索フラグ 正規表現パターン 属性一覧 メソッド一覧 検索 全検索 全検索 (イテレータ) 分割 置換 キャプチャ参照 マッチ位置インデックス
メモ
- 正規表現操作モジュール
- 正規表現パターンを使用し、文字列の検索・置換・分割を行う
- 正規表現パターンは、通常 raw 文字列記法 を使用 ( "\\w\\\\" → r"\w\\" )
- 正規表現パターン・検索元文字列は、文字列 (str)・バイト文字列 (bytes)
- モジュールコンテンツ・オブジェクト
- 同一の正規表現パターンで複数回処理する場合、Pattern【正規表現オブジェクト】を使用
但し、数回の使用であれば、re【正規表現モジュールコンテンツ】のキャッシュで十分 - Match【マッチオブジェクト】で検索結果を処理
モジュールコンテンツ・オブジェクト 備考 re【正規表現モジュールコンテンツ】 Pattern【正規表現オブジェクト】 re.compile()【コンパイル】で返却 Match【マッチオブジェクト】 下記で返却
fullmatch()【検索 (全体一致)】
match()【検索 (先頭一致)】
search()【検索】
- 同一の正規表現パターンで複数回処理する場合、Pattern【正規表現オブジェクト】を使用
- 関連
検索フラグ・正規表現パターン
| 検索フラグ | 備考 |
|---|---|
| re.A re.ASCII | ASCII 文字限定 |
| re.DEBUG | デバッグ情報表示 |
| re.I re.IGNORECASE | 大小文字の区別なし |
| re.L re.LOCALE | 大小文字の区別なし (ロケール) ( バイト文字列 (bytes)のみ可 3.6 ) |
| re.M re.MULTILINE | 複数行検索 ( ^【先頭】・$【末尾】を各行に対応 ) |
| re.S re.DOTALL | 改行検索 ( .【改行以外の文字】を改行も対応 ) |
| re.U re.UNICODE | Unicode 対応 (後方互換の為、冗長) |
| re.X re.VERBOSE | コメント対応 |
| 正規表現パターン | 備考 |
|---|---|
| . | 改行以外の文字 ( DOTALL【改行検索】指定:改行を含む ) |
| ^ | 先頭 ( MULTILINE【複数行検索】指定:各行の先頭を含む ) |
| $ | 末尾 ( MULTILINE【複数行検索】指定:各行の末尾を含む ) |
| パターン* | パターンの 0 回以上の繰り返し ( 最長一致 ) |
| パターン*? | パターンの 0 回以上の繰り返し ( 最短一致 ) |
| パターン+ | パターンの 1 回以上の繰り返し ( 最長一致 ) |
| パターン+? | パターンの 1 回以上の繰り返し ( 最短一致 ) |
| パターン? | パターンの 0 回か 1 回 ( 最長一致 ) |
| パターン?? | パターンの 0 回か 1 回 ( 最短一致 ) |
| パターン{m} | パターンの m 回の繰り返し ( m:正数 ) |
| パターン{m,n} | パターンの m 回 ~ n 回の繰り返し ( 最長一致 ) ( m・n:正数 / m または n は省略可能 ) |
| パターン{m,n}? | パターンの m 回 ~ n 回の繰り返し ( 最短一致 ) ( m・n:正数 / m または n は省略可能 ) |
| [文字の集合] | 集合:文字の集合 内の文字 ( - で範囲指定 ) |
| [^文字の集合] | 補集合:文字の集合 内の文字以外 |
| パターンA|パターンB | パターンを区切り、どちらかのパターンと一致 |
| (パターン) | キャプチャ (後方参照:\キャプチャ番号) |
| (?:パターン) | キャプチャ (後方参照不可) |
| (?P<キャプチャ名> パターン) | 名前付きキャプチャ ( 後方参照:(?P=キャプチャ名) ・\キャプチャ番号 ) |
| (?P=キャプチャ名) | 名前付きキャプチャの後方参照 ( 名前付きキャプチャ: (?P<キャプチャ名>パターン) ) |
| (?(キャプチャ番号 / キャプチャ名) 存在ありパターン |存在なしパターン) | キャプチャ存在判定 存在なしパターン は省略可 ( 対になった括弧のチェック等に有用 ) |
| (?#コメント) | コメント |
| パターン(?=パターン (後)) | 肯定先読み:パターン に パターン (後) が続く |
| パターン(?!パターン (後)) | 否定先読み:パターン に パターン (後) が続かない |
| (?<=パターン (前))パターン | 肯定後読み:パターン (前) に パターン が続く |
| (?<!パターン (前))パターン | 否定後読み:パターン (前) でない後に パターン が続く |
| (?aiLmsux) | 検索フラグ指定 ( 全体 ) aiLmsux の1個以上を指定 (以下に対応) ・re.A【ASCII 文字限定】 ・re.I【大小文字の区別なし】 ・re.L【大小文字の区別なし (ロケール)】 ・re.M【複数行検索】 ・re.S【改行検索】 ・re.U【Unicode 対応 (後方互換)】 ・re.X【コメント対応】 |
| (?aiLmsux-imsx: パターン) 3.6 | 検索フラグ設定・解除 ( 個別 ) 設定する aiLmsux の0個以上 - 解除する imsx の1個以上を指定 (省略可) 対応フラグ:(?aiLmsux) 参照 ( aLu 追加 3.7 ) |
| \~ | エスケープシーケンス (以下参照) 未定義の ASCII 文字指定はエラー 3.6 |
| \ooo | 8進数定義 (先頭が0 または 3桁) |
| \キャプチャ番号 (1~99) | キャプチャの後方参照 |
| \\ | バックスラッシュ |
| \a | ビープ音 (\u0007) |
| \A | 文字列の先頭 |
| \b | 単語の区切り ( アンダースコア(_) を含む ) ( ASCII フラグ指定:ASCII 限定 ) |
| \B | 単語の区切り以外 ( アンダースコア(_) を含まない ) ( ASCII フラグ指定:ASCII 限定 ) |
| \d | 文字列 (str):10進数 ( ASCII フラグ指定:ASCII 限定 / [0-9] と等価 ) バイト文字列 (bytes):10 進数 ( [0-9] と等価 ) |
| \D | 非 10 進数 (\d の逆) ( ASCII フラグ指定:ASCII 限定 / [^0-9] と等価 ) |
| \f | 改ページ (\u000C) |
| \n | 改行 (\u000A) |
| \N{Unicode 文字名} 3.8 | Unicode 文字 (例:\N{COPYRIGHT SIGN} ) |
| \r | 復帰 (\u000D) |
| \s | 文字列 (str):空白文字 ( ASCII フラグ指定:ASCII 限定 / [ \t\n\r\f\v] と等価 ) バイト文字列 (bytes):空白文字 ( [ \t\n\r\f\v] と等価 ) |
| \S | 空白文字以外 (\s の逆) ( ASCII フラグ指定:ASCII 限定 / [^ \t\n\r\f\v] と等価 ) |
| \t | タブ (\u0009) |
| \uhhhh 3.3 | 16進数定義 (16 bit) バイト文字列 (bytes):エラー |
| \Uhhhhhhhh 3.3 | 16進数定義 (32 bit) バイト文字列 (bytes):エラー |
| \v | 垂直タブ (\u000B) |
| \w | 文字列 (str):単語文字 ( 文字・数字・アンダースコア(_) ) ( ASCII フラグ指定:ASCII 限定 / [a-zA-Z0-9_] と等価 ) バイト文字列 (bytes):[a-zA-Z0-9_] と等価 |
| \W | 単語文字以外 ( \w の逆 ) |
| \xhh | 16進数定義 (8 bit) |
| \Z | 文字列の末尾 |
パターン:検索位置移動
パターン:検索位置移動なし
属性・メソッド・例外クラス
| 属性 | 備考 |
|---|---|
| Pattern | |
| Pattern.flags【検索フラグ】 | 参考:検索フラグ |
| Pattern.groups【キャプチャ数】 | |
| Pattern.groupindex【キャプチャ名辞書】 | キャプチャ名・キャプチャ番号 |
| Pattern.pattern【正規表現パターン】 | |
| Match | |
| Match.endpos【検索最終位置インデックス】 | |
| Match.lastindex【最終キャプチャ番号】 | |
| Match.lastgroup【最終キャプチャ名】 | 不一致 または キャプチャ名が未指定:None |
| Match.pos【検索開始位置インデックス】 | |
| Match.re【正規表現オブジェクト】 | |
| Match.string【検索元文字列】 | |
| メソッド | re | Pattern | Match | 戻り値 |
|---|---|---|---|---|
| __getitem__()【キャプチャ参照 (添字参照)】 | ● | マッチ文字列 | ||
| compile()【コンパイル】 | ● | Pattern【正規表現オブジェクト】 | ||
| end()【マッチ位置インデックス (末尾)】 | ● | 末尾のマッチ位置インデックス | ||
| escape()【特殊文字エスケープ】 | ● | エスケープ後の文字列 | ||
| expand()【キャプチャ参照展開】 | ● | 展開後の文字列 | ||
| findall()【全検索】 | ● | ● | 検索結果リスト | |
| finditer()【全検索 (イテレータ)】 | ● | ● | イテレータ (Match【マッチオブジェクト】) | |
| fullmatch()【検索 (全体一致)】 | ● | ● | Match【マッチオブジェクト】 | |
| group()【キャプチャ参照】 | ● | 引数0個:マッチ文字列全体 引数1個:マッチ文字列 引数2個以上:マッチ文字列のタプル | ||
| groupdict()【キャプチャ参照 (辞書)】 | ● | 辞書 (キャプチャ名・キャプチャ値) | ||
| groups()【キャプチャ参照 (タプル)】 | ● | タプル (キャプチャ値) | ||
| match()【検索 (先頭一致)】 | ● | ● | Match【マッチオブジェクト】 | |
| purge()【正規表現キャッシュ クリア】 | ● | なし | ||
| search()【検索】 | ● | ● | Match【マッチオブジェクト】 | |
| span()【マッチ位置インデックス】 | ● | マッチ位置インデックス (タプル:先頭・末尾) | ||
| split()【分割】 | ● | ● | 分割リスト | |
| start()【マッチ位置インデックス (先頭)】 | ● | 先頭のマッチ位置インデックス | ||
| sub()【置換】 | ● | ● | 置換後の文字列 | |
| subn()【置換・置換個数取得】 | ● | ● | タプル (置換後の文字列・置換個数) |
| 例外クラス | 属性 3.5 | 備考 |
|---|---|---|
| re.error(msg, pattern=None, pos=None) | msg | エラーメッセージ |
| pattern | 正規表現パターン | |
| pos | エラー発生位置インデックス | |
| lineno | pos に対応する行 | |
| colno | pos に対応する列 |
compile()【コンパイル】
メモ
- コンパイル
- 同一の正規表現パターンで複数回処理する場合、 コンパイルしてPattern【正規表現オブジェクト】を使用
但し、数回の使用であれば、re【正規表現モジュールコンテンツ】のキャッシュで十分
- 同一の正規表現パターンで複数回処理する場合、 コンパイルしてPattern【正規表現オブジェクト】を使用
- 関連
構文
re.compile(pattern, flags=0)
戻り値Pattern【正規表現オブジェクト】
pattern正規表現パターン
flags検索フラグ
例外re.error
例
import re
s1 = "The quick brown fox jumps over the lazy dog."
s2 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
pattern = re.compile(r"(\w+) (\w+)")
print(pattern.search(s1))
print(pattern.search(s2))
【出力例】
<re.Match object; span=(0, 9), match='The quick'>
<re.Match object; span=(0, 9), match='THE QUICK'>
search()【検索】・match()【検索 (先頭一致)】・fullmatch()【検索 (全体一致)】3.4
メモ
- 正規表現パターンで検索
- 任意の部分が一致する検索:search()【検索】
- 先頭が一致する検索:match()【検索 (先頭一致)】
- 全体が一致する検索:fullmatch()【検索 (全体一致)】
- 関連
構文
re.search(pattern, string, flags=0)
re.match(pattern, string, flags=0)
re.fullmatch(pattern, string, flags=0) 3.4
Pattern.search(string[, pos[, endpos]])
Pattern.match(string[, pos[, endpos]])
Pattern.fullmatch(string[, pos[, endpos]]) 3.4
戻り値Match【マッチオブジェクト】 (一致無し:None)
pattern正規表現パターン
string検索元文字列
flags検索フラグ
pos検索開始位置インデックス (省略:0)
endpos検索終了位置インデックス
例外re.error
例
import re
s = "The quick brown fox jumps over the lazy dog."
print(re.search(r"\w*x\w*", s))
print(re.search(r"\w*y\w*", s))
print(re.search(r"\w*z\w*", s))
print(re.search(r"\w*@\w*", s))
pattern = re.compile(r"\w+")
match = pattern.search(s)
print(match)
match = pattern.search(s, match.end())
print(match)
match = pattern.search(s, match.end())
print(match)
【出力例】
<re.Match object; span=(16, 19), match='fox'>
<re.Match object; span=(35, 39), match='lazy'>
<re.Match object; span=(35, 39), match='lazy'>
None
<re.Match object; span=(0, 3), match='The'>
<re.Match object; span=(4, 9), match='quick'>
<re.Match object; span=(10, 15), match='brown'>
import re
s = "The quick brown fox jumps over the lazy dog."
p1 = r"\w*h\w*"
p2 = r"\w*x\w*"
print(re.match(p1, s))
print(re.match(p2, s))
pattern = re.compile(p1)
print(pattern.match(s))
pattern = re.compile(p2)
print(pattern.match(s))
【出力例】
<re.Match object; span=(0, 3), match='The'>
None
<re.Match object; span=(0, 3), match='The'>
None
import re
s = "abc_xyz"
p1 = r"a\w*z"
p2 = r"z\w*a"
print(re.fullmatch(p1, s))
print(re.fullmatch(p2, s))
pattern = re.compile(p1)
print(pattern.fullmatch(s))
pattern = re.compile(p2)
print(pattern.fullmatch(s))
【出力例】
<re.Match object; span=(0, 7), match='abc_xyz'>
None
<re.Match object; span=(0, 7), match='abc_xyz'>
None
split()【分割】
メモ
構文
例
import re
s = "The quick brown fox jumps over the lazy dog."
print(re.split(" ", s))
print(re.split(" ", s, 3))
pattern = re.compile(" ")
print(pattern.split(s))
print(pattern.split(s, 3))
【出力例】
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
['The', 'quick', 'brown', 'fox jumps over the lazy dog.']
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
['The', 'quick', 'brown', 'fox jumps over the lazy dog.']
findall()【全検索】
メモ
構文
例
import re
s = "The quick brown fox jumps over the lazy dog."
print(re.findall(r"\w+", s))
print(re.findall(r"(\w+)", s))
print(re.findall(r"(\w+) (\w+)", s))
pattern = re.compile(r"\w+")
print(pattern.findall(s))
print(pattern.findall(s, 4, 19))
【出力例】
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
[('The', 'quick'), ('brown', 'fox'), ('jumps', 'over'), ('the', 'lazy')]
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
['quick', 'brown', 'fox']
finditer()【全検索 (イテレータ)】
メモ
構文
re.finditer(pattern, string, flags=0)
Pattern.finditer(string[, pos[, endpos]])
戻り値イテレータ (Match【マッチオブジェクト】)
pattern正規表現パターン
string検索元文字列
flags検索フラグ
pos検索開始位置インデックス (省略:0)
endpos検索終了位置インデックス
例外re.error
例
import re
s = "The quick brown fox jumps over the lazy dog."
print("re.finditer()")
for item in re.finditer(r"\w+", s):
print(item)
print("pattern.finditer()")
pattern = re.compile(r"\w+")
for item in pattern.finditer(s):
print(item)
print("pattern.finditer(, 4, 19)")
for item in pattern.finditer(s, 4, 19):
print(item)
【出力例】
re.finditer()
<re.Match object; span=(0, 3), match='The'>
<re.Match object; span=(4, 9), match='quick'>
<re.Match object; span=(10, 15), match='brown'>
<re.Match object; span=(16, 19), match='fox'>
<re.Match object; span=(20, 25), match='jumps'>
<re.Match object; span=(26, 30), match='over'>
<re.Match object; span=(31, 34), match='the'>
<re.Match object; span=(35, 39), match='lazy'>
<re.Match object; span=(40, 43), match='dog'>
pattern.finditer()
<re.Match object; span=(0, 3), match='The'>
<re.Match object; span=(4, 9), match='quick'>
<re.Match object; span=(10, 15), match='brown'>
<re.Match object; span=(16, 19), match='fox'>
<re.Match object; span=(20, 25), match='jumps'>
<re.Match object; span=(26, 30), match='over'>
<re.Match object; span=(31, 34), match='the'>
<re.Match object; span=(35, 39), match='lazy'>
<re.Match object; span=(40, 43), match='dog'>
pattern.finditer(, 4, 19)
<re.Match object; span=(4, 9), match='quick'>
<re.Match object; span=(10, 15), match='brown'>
<re.Match object; span=(16, 19), match='fox'>
sub()【置換】・subn()【置換・置換個数取得】
メモ
- 正規表現パターンを使用して置換
- キャプチャの参照
置換文字列内:\g<name>・\g<number> ・\number
置換関数内:Match.group()【キャプチャ参照】
- キャプチャの参照
- 関連
構文
re.sub(pattern, repl, string, count=0, flags=0 3.1)
re.subn(pattern, repl, string, count=0, flags=0 3.1)
Pattern.sub(repl, string, count=0)
Pattern.subn(repl, string, count=0)
戻り値処理結果
sub()置換後の文字列
subn()タプル (置換後の文字列, 置換個数)
pattern置換パターン (正規表現パターン・Pattern【正規表現オブジェクト】)
repl置換文字列 または 置換文字列を返却する置換関数 (引数:Match【マッチオブジェクト】)
(キャプチャの参照については、メモ 参照)
string置換元文字列
count最大置換数 (0:全置換)
flags検索フラグ
例外re.error
例
import re
s = "The quick brown fox jumps over the lazy dog."
p = r"\s"
repl = " / "
print(re.sub(p, repl, s))
print(re.subn(p, repl, s, 3))
pattern = re.compile(p)
print(pattern.sub(repl, s))
print(pattern.subn(repl, s, 3))
【出力例】
The / quick / brown / fox / jumps / over / the / lazy / dog.
('The / quick / brown / fox jumps over the lazy dog.', 3)
The / quick / brown / fox / jumps / over / the / lazy / dog.
('The / quick / brown / fox jumps over the lazy dog.', 3)
import re
s = "The quick brown fox jumps over the lazy dog."
p = r"(\w+) (?P<group2>\w+) (\w+)"
repl = r"[\3 \g<group2> \g<1>]"
print(re.sub(p, repl, s))
print(re.subn(p, repl, s, 2))
pattern = re.compile(p)
print(pattern.sub(repl, s))
print(pattern.subn(repl, s, 2))
【出力例】
[brown quick The] [over jumps fox] [dog lazy the].
('[brown quick The] [over jumps fox] the lazy dog.', 2)
[brown quick The] [over jumps fox] [dog lazy the].
('[brown quick The] [over jumps fox] the lazy dog.', 2)
import re
def repl(match):
s = match.group(0)
return s + "(" + str(len(s)) + ")"
s = "The quick brown fox jumps over the lazy dog."
p = r"\w+"
print(re.sub(p, repl, s))
print(re.subn(p, repl, s, 3))
pattern = re.compile(p)
print(pattern.sub(repl, s))
print(pattern.subn(repl, s, 3))
【出力例】
The(3) quick(5) brown(5) fox(3) jumps(5) over(4) the(3) lazy(4) dog(3).
('The(3) quick(5) brown(5) fox jumps over the lazy dog.', 3)
The(3) quick(5) brown(5) fox(3) jumps(5) over(4) the(3) lazy(4) dog(3).
('The(3) quick(5) brown(5) fox jumps over the lazy dog.', 3)
import re
def repl(match):
return "[" + match.group("group2") + " " + match.group(1) + "]"
s = "The quick brown fox jumps over the lazy dog."
p = r"(\w+) (?P<group2>\w+)"
pattern = re.compile(p)
print("(A)", re.sub(p, repl, s))
print("(B)", re.sub(pattern, repl, s))
print("(C)", pattern.sub(repl, s))
print("(D)", re.subn(p, repl, s, 2))
print("(E)", re.subn(pattern, repl, s, 2))
print("(F)", pattern.subn(repl, s, 2))
【出力例】
(A) [quick The] [fox brown] [over jumps] [lazy the] dog.
(B) [quick The] [fox brown] [over jumps] [lazy the] dog.
(C) [quick The] [fox brown] [over jumps] [lazy the] dog.
(D) ('[quick The] [fox brown] jumps over the lazy dog.', 2)
(E) ('[quick The] [fox brown] jumps over the lazy dog.', 2)
(F) ('[quick The] [fox brown] jumps over the lazy dog.', 2)
escape()【特殊文字エスケープ】
メモ
- 特殊文字のエスケープ
- 関連
構文
re.escape(pattern)
戻り値エスケープ後の文字列
pattern文字列
例
import re
print(re.escape("()[].+*?"))
【出力例】
\(\)\[\]\.\+\*\?
purge()【正規表現キャッシュ クリア】
メモ
- 正規表現キャッシュのクリア
- 関連
構文
re.purge()
戻り値なし
Match.expand()【キャプチャ参照展開】3.6
メモ
- キャプチャ参照の展開
- キャプチャの参照: \g<name>・\g<number>・\number
- 関連
構文
例
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match)
print(match.expand(r"\g<group4> \g<3> \g<group2> \1"))
【出力例】
<re.Match object; span=(0, 19), match='The quick brown fox'>
fox brown quick The
Match.__getitem__()【キャプチャ参照 (添字参照)】3.6
Match.group()【キャプチャ参照】
Match.groupdict()【キャプチャ参照 (辞書)】
Match.groups()【キャプチャ参照 (タプル)】
メモ
構文
Match.__getitem__(g) 3.6
戻り値キャプチャ値
gキャプチャ番号 または キャプチャ名
例外IndexError
Match.group([group1, ...])
戻り値
引数0個:マッチ文字列全体
引数1個:キャプチャ値
引数2個以上:キャプチャ値のタプル
groupNキャプチャ番号 または キャプチャ名
例外IndexError
Match.groupdict(default=None)
戻り値辞書 (キャプチャ名, キャプチャ値)
default一致しないキャプチャの対応値
Match.groups(default=None)
戻り値タプル (キャプチャ値)
default一致しないキャプチャの対応値
例
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match[0])
print(match[1])
print(match[2])
print(match[3])
print(match["group2"])
print(match["group4"])
【出力例】
The quick brown fox
The
quick
brown
quick
fox
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.group(3))
print(match.group("group2"))
print(match.group("group4"))
print(match.group(3, "group4"))
【出力例】
The quick brown fox
The
quick
brown
quick
fox
('brown', 'fox')
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match.groupdict())
match = re.search(r"(?P<group1>\w+) (?P<group2>\w+) (?P<group9>xyz\w+)?", s)
print(match.groupdict())
print(match.groupdict("EMPTY"))
【出力例】
{'group2': 'quick', 'group4': 'fox'}
{'group1': 'The', 'group2': 'quick', 'group9': None}
{'group1': 'The', 'group2': 'quick', 'group9': 'EMPTY'}
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match.groups())
match = re.search(r"(?P<group1>\w+) (?P<group2>\w+) (?P<group9>xyz\w+)?", s)
print(match.groups())
print(match.groups("EMPTY"))
【出力例】
('The', 'quick', 'brown', 'fox')
('The', 'quick', None)
('The', 'quick', 'EMPTY')
Match.start()【マッチ位置インデックス (先頭)】・Match.end()【マッチ位置インデックス (末尾)】 ・Match.span()【マッチ位置インデックス】
メモ
- マッチ位置のインデックスを取得
- 関連
構文
Match.start([group])
Match.end([group])
戻り値先頭または末尾のマッチ位置インデックス
groupキャプチャ番号 または キャプチャ名
Match.span([group])
戻り値タプル (先頭マッチ位置インデックス, 末尾マッチ位置インデックス)
groupキャプチャ番号 または キャプチャ名 (省略 / 0:マッチ部分全体)
例
import re
s = "The quick brown fox jumps over the lazy dog."
match = re.search(r"\w*c\w*", s)
print(match)
print(match.start())
print(match.end())
print(match.span())
print()
match = re.search(r"(\w+) (?P<group2>\w+) (\w+) (?P<group4>\w+)", s)
print(match)
print(match.start())
print(match.end())
print(match.span())
print()
print(match.start(1))
print(match.end(1))
print(match.span(1))
print()
print(match.start("group4"))
print(match.end("group4"))
print(match.span("group4"))
【出力例】
<re.Match object; span=(4, 9), match='quick'>
4
9
(4, 9)
<re.Match object; span=(0, 19), match='The quick brown fox'>
0
19
(0, 19)
0
3
(0, 3)
16
19
(16, 19)