Luyện tập câu hỏi phỏng vấn lập trình bằng Python
Kirill Smirnov
Data Science Consultant, Altran
Biểu thức chính quy (regex) - chuỗi ký tự đặc biệt (metacharacter) xác định mẫu để tìm trong văn bản.
cat
"Tôi có một con mèo. Mèo của tôi thích ăn nhiều. Nó cũng bắt chuột."
Biểu thức chính quy (regex) - chuỗi ký tự đặc biệt (metacharacter) xác định mẫu để tìm trong văn bản.
cat
"Tôi có một con mèo. Mèo của tôi thích ăn nhiều. Nó cũng bắt mèo chuột."
Ví dụ:
[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected].
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
Ký tự và số đơn giản ánh xạ chính nó:
a $\rightarrow$ aA $\rightarrow$ A1 $\rightarrow$ 1Dấu chấm khớp với bất kỳ ký tự nào:
. $\rightarrow$ bất kỳ ký tự nào. $\rightarrow$ 'a', '1', '"', ' ', ...
\. $\rightarrow$ .Các metacharacter sau biểu diễn \ theo sau bởi một chữ cái:
\w $\rightarrow$ bất kỳ ký tự chữ-số hoặc dấu gạch dưới\w $\rightarrow$ '1', 'a','_', ...
\d $\rightarrow$ bất kỳ chữ số nào\d $\rightarrow$ '1', '2','3', ...
\s $\rightarrow$ bất kỳ ký tự khoảng trắng nào\s $\rightarrow$ ' ', '\t', ...
Một số metacharacter có thể đặt trong ngoặc vuông:
[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$ không có ký tự hoặc lặp lại số lần không xác địnha* $\rightarrow$ '', 'a', 'aa', ...
+ $\rightarrow$ xuất hiện ít nhất một lầna+ $\rightarrow$ 'a', 'aa', 'aaa', ...
? $\rightarrow$ có thể có hoặc không có ký tựa? $\rightarrow$ '', 'a'
{n, m} $\rightarrow$ xuất hiện từ n đến m lầna{2, 4} $\rightarrow$ 'aa', 'aaa', 'aaaa'
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[\w\.]+ $\rightarrow$ john.smith, boss, info
ít nhất một chữ, số, dấu gạch dưới hoặc dấu chấm
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
@ $\rightarrow$ @
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ mailbox, company
ít nhất một chữ thường
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
\. $\rightarrow$ .
Ví dụ:
**[email protected] là email của John. Ông ấy thường viết cho sếp tại [email protected]. Nhưng thư được chuyển tiếp cho thư ký tại [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ com
ít nhất một chữ thường
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 ',
'.']
Luyện tập câu hỏi phỏng vấn lập trình bằng Python