如何在 Python 撰寫正規表示式?

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.」

Python 程式面試題實作練習

定義

Regular expression(正規表示式):由特殊字元(metacharacters)組成,用來定義在文字中搜尋的樣式。

 

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

點號可配對任意字元:

  • . $\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$ aAbB
  • [a-z] $\rightarrow$ abc、...
  • [A-Z] $\rightarrow$ ABC、...
  • [0-9] $\rightarrow$ 012、...
  • [A-Za-z] $\rightarrow$ ABC、...、abc、...
Python 程式面試題實作練習

重複次數

  • * $\rightarrow$ 出現 0 次或重複不定次數

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

  • + $\rightarrow$ 至少出現 1 次

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

  • ? $\rightarrow$ 可有可無

a? $\rightarrow$ '''a'

  • {n, m} $\rightarrow$ 介於 nm

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.smithbossinfo

至少 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$ mailboxcompany

至少 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 程式面試題實作練習

一起來練習吧!

Python 程式面試題實作練習

Preparing Video For Download...