2020年12月1日火曜日

Pythonにおける正規表現

 言語処理100本ノックの問題03に正規表現ライブラリ「re」を使ったので、少し練習してみる。

1.match(pattern, string):文字列の先頭で正規表現とマッチするか判定します。

import re

string = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
pattern = 'Now'

print(re.match(pattern, string))

出力

<re.Match object; span=(0, 3), match='Now'> 

「文章最初の1文字目から3文字目の間(span=(0,3))に'Now'がある。」と返してくる。


patternをNow以外、例えば、"need"に変更して実行してみる。

出力

None

Noneと返す。(Faulseとかじゃないんだね。)


2.search(pattern, string):文字列を走査して、正規表現がどこにマッチするか調べる。

import re

string = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
pattern = 'alcoholic'

print(re.search(pattern, string))


出力

<re.Match object; span=(20, 29), match='alcoholic'>


3.findall(pattern, string):正規表現にマッチする部分文字列を全て探し出してリストとして返します。

 import re

string = 'John, while Mary had had had had had, had had had had. had had had, had had had had had, had had had had. had had had had. had had had, had had had had. had had had had had the higher number of hads so John had won.'
pattern = 'had'

print(re.findall(pattern, string))

 出力

 ['had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had', 'had']

4.finditer(pattern, string):正規表現にマッチする部分文字列を全て探し出してiteratorとして返します。

 import re

string = 'John, while Mary had had had had had, had had had had. had had had, had had had had had, had had had had. had had had had. had had had, had had had had. had had had had had the higher number of hads so John had won.'
pattern = 'had'

print(re.finditer(pattern, string))

 出力

<callable_iterator object at 0x0000013CE37A0910>

 意味が分からないので調べたのち、改めて書きたいと思います。





参考リンク

Pythonでの正規表現の使い方 


 


0 件のコメント:

コメントを投稿