Pythonで正規表現を書くには?

Pythonでコーディング面接問題を練習する

Kirill Smirnov

Data Science Consultant, Altran

定義

正規表現 — テキスト中で検索するパターンを定義する特殊文字(メタ文字)の並び。

 

cat

"I have a cat. My cat likes to eat a lot. It also catches mice."

Pythonでコーディング面接問題を練習する

定義

正規表現 — テキスト中で検索するパターンを定義する特殊文字(メタ文字)の並び。

 

cat

"I have a cat. My cat likes to eat a lot. It also catches mice."

Pythonでコーディング面接問題を練習する

複雑なパターン

例:

[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected] に転送されます。

Pythonでコーディング面接問題を練習する

複雑なパターン

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

Pythonでコーディング面接問題を練習する

特殊文字

英字や数字などの単純な文字はそのまま一致します:

  • a $\rightarrow$ a
  • A $\rightarrow$ A
  • 1 $\rightarrow$ 1

ドットは任意の1文字に一致:

  • . $\rightarrow$ 任意の文字

. $\rightarrow$ 'a', '1', '"', ' ', ...

  • \. $\rightarrow$ .
Pythonでコーディング面接問題を練習する

特殊文字

次のメタ文字は、\ と英字の組み合わせです:

  • \w $\rightarrow$ 英数字またはアンダースコア

\w $\rightarrow$ '1', 'a','_', ...

  • \d $\rightarrow$ 数字

\d $\rightarrow$ '1', '2','3', ...

  • \s $\rightarrow$ 空白文字

\s $\rightarrow$ ' ', '\t', ...

Pythonでコーディング面接問題を練習する

角かっこ

いくつかのメタ文字は角かっこで囲めます:

  • [aAbB] $\rightarrow$ a, A, b, B
  • [a-z] $\rightarrow$ a, b, c, ...
  • [A-Z] $\rightarrow$ A, B, C, ...
  • [0-9] $\rightarrow$ 0, 1, 2, ...
  • [A-Za-z] $\rightarrow$ A, B, C, ..., a, b, c, ...
Pythonでコーディング面接問題を練習する

繰り返し指定子

  • * $\rightarrow$ 0回または不定回数の繰り返し

a* $\rightarrow$ '', 'a', 'aa', ...

  • + $\rightarrow$ 1回以上の出現

a+ $\rightarrow$ 'a', 'aa', 'aaa', ...

  • ? $\rightarrow$ 出現してもしなくてもよい

a? $\rightarrow$ '', 'a'

  • {n, m} $\rightarrow$ n〜m回の出現

a{2, 4} $\rightarrow$ 'aa', 'aaa', 'aaaa'

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

[\w\.]+@[a-z]+\.[a-z]+

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

`[\w.]+'@[a-z]+\.[a-z]+

 

[\w\.]+' $\rightarrow$john.smith,boss,info`

英数字、アンダースコア、ドットを1文字以上

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

[\w\.]+'**@**[a-z]+.[a-z]+`

 

@ $\rightarrow$ @

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

[\w\.]+'@**[a-z]+**.[a-z]+`

 

[a-z]+ $\rightarrow$ mailbox, company

小文字の英字を1文字以上

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

[\w\.]+'@[a-z]+**.**[a-z]+`

 

\. $\rightarrow$ .

Pythonでコーディング面接問題を練習する

メールの正規表現

例:

**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。

[\w\.]+'@[a-z]+\.[a-z]+

 

[a-z]+ $\rightarrow$ com

小文字の英字を1文字以上

Pythonでコーディング面接問題を練習する

re パッケージ

import re

pattern = re.compile(r'[\w\.]+@[a-z]+\.[a-z]+')
text = '[email protected] is the e-mail of '\
'John. He often writes to his boss at '\
'[email protected]. But the messages get forwarded '\
'to his secretary at [email protected].'
Pythonでコーディング面接問題を練習する

re.finditer()

result = re.finditer(pattern, text)
print(result)
<callable_iterator object at 0x7f5dff81af98>
for match in result:
    print(match)
<_sre.SRE_Match object; span=(0, 22), match='[email protected]'>
<_sre.SRE_Match object; span=(77, 93), match='[email protected]'>
<_sre.SRE_Match object; span=(146, 162), match='[email protected]'>
Pythonでコーディング面接問題を練習する

re.finditer()

result = re.finditer(pattern, text)
print(result)
<callable_iterator object ...>
for match in result:
    print(match.group())
    print(match.start())
    print(match.end())
Pythonでコーディング面接問題を練習する

re.findall()

substrings = re.findall(pattern, text)
print(substrings)
['[email protected]', '[email protected]', '[email protected]']
Pythonでコーディング面接問題を練習する

re.split()

split_list = re.split(pattern, text)
print(split_list)
['',
 ' is the e-mail of John. He often writes to his boss at ',
 '. But the messages get forwarded to his secretary at ',
 '.']
Pythonでコーディング面接問題を練習する

Ayo berlatih!

Pythonでコーディング面接問題を練習する

Preparing Video For Download...