Pythonでコーディング面接問題を練習する
Kirill Smirnov
Data Science Consultant, Altran
正規表現 — テキスト中で検索するパターンを定義する特殊文字(メタ文字)の並び。
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
正規表現 — テキスト中で検索するパターンを定義する特殊文字(メタ文字)の並び。
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
例:
[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected] に転送されます。
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
英字や数字などの単純な文字はそのまま一致します:
a $\rightarrow$ aA $\rightarrow$ A1 $\rightarrow$ 1ドットは任意の1文字に一致:
. $\rightarrow$ 任意の文字. $\rightarrow$ 'a', '1', '"', ' ', ...
\. $\rightarrow$ .次のメタ文字は、\ と英字の組み合わせです:
\w $\rightarrow$ 英数字またはアンダースコア\w $\rightarrow$ '1', 'a','_', ...
\d $\rightarrow$ 数字\d $\rightarrow$ '1', '2','3', ...
\s $\rightarrow$ 空白文字\s $\rightarrow$ ' ', '\t', ...
いくつかのメタ文字は角かっこで囲めます:
[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, ...* $\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'
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
[\w\.]+@[a-z]+\.[a-z]+
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
`[\w.]+'@[a-z]+\.[a-z]+
[\w\.]+' $\rightarrow$john.smith,boss,info`
英数字、アンダースコア、ドットを1文字以上
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
[\w\.]+'**@**[a-z]+.[a-z]+`
@ $\rightarrow$ @
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
[\w\.]+'@**[a-z]+**.[a-z]+`
[a-z]+ $\rightarrow$ mailbox, company
小文字の英字を1文字以上
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
[\w\.]+'@[a-z]+**.**[a-z]+`
\. $\rightarrow$ .
例:
**[email protected] はJohnのメールです。彼はよく上司の [email protected] に書きます。メッセージは秘書の [email protected]** に転送されます。
[\w\.]+'@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ com
小文字の英字を1文字以上
import repattern = 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].'
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]'>
result = re.finditer(pattern, text)
print(result)
<callable_iterator object ...>
for match in result:
print(match.group())
print(match.start())
print(match.end())
[email protected]
0
22
[email protected]
77
93
[email protected]
146
162
substrings = re.findall(pattern, text)
print(substrings)
['[email protected]', '[email protected]', '[email protected]']
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でコーディング面接問題を練習する