pythonで正規表現を使って文字列を抽出するには「re.search」と「re.findall」

pythonの以下コマンドを使って、文字列を抽出する方法

 re.search()

 re.findall()

 ・re.search() 実行例

import re

test = "test and test taro"

#「test」から"tes"という文字列を検索する
re.search("tes",test)
re.Match object; span=(0, 3), match='tes'>

注:最初にマッチした文字列を返す

 ・re.findall() 実行例

import re

test = "test and test taro"

#「test」から"tes"という文字列を検索する
re.findall("tes",test)
['tes', 'tes']

結果はマッチする全ての文字列を返す


返信を残す

メールアドレスが公開されることはありません。

CAPTCHA