Python 程式面試題實作練習
Kirill Smirnov
Data Science Consultant, Altran
Regular expression(正規表示式):由特殊字元(metacharacters)組成,用來定義在文字中搜尋的樣式。
cat
「I have a cat. My cat likes to eat a lot. It also catches mice.」
Regular expression(正規表示式):由特殊字元(metacharacters)組成,用來定義在文字中搜尋的樣式。
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點號可配對任意字元:
. $\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 程式面試題實作練習