0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

退屈なことはPythonにやらせよう 勉強記録 【正規表現 ()のパターンマッチ】

Last updated at Posted at 2019-08-05

###パターンマッチ機能
●丸かっこを用いたグルーピング
電話番号
(\d\d\d)-(\d\d\d-\d\d\d\d)

グループ1:(\d\d\d)
グループ2:(\d\d\d-\d\d\d\d)

import re
phone_num_regex =re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phone_num_regex.search('電話番号は415-555-4242です')
mo = group(1) 
mo = group(2)
mo = group(0)
mo = group()
mo = groups()

解答
mo =group(1)
'415'

mo= group(2)
'555-4242'

mo = group(0)
'415-555-4242'

mo = group()
'415-555-4242'

mo = groups()
('415', '555-4242')

group(0),group()ではマッチした文字列全体を返す。
groups()はすべてのグループを一度に取得したいとき

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?