ฝึกทำโจทย์สัมภาษณ์งานเขียนโค้ดด้วย 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$ .metacharacters ต่อไปนี้แทน \ ตามด้วยตัวอักษร:
\w $\rightarrow$ อักขระตัวเลข ตัวอักษร หรือขีดล่าง\w $\rightarrow$ '1', 'a','_', ...
\d $\rightarrow$ ตัวเลขใดก็ได้\d $\rightarrow$ '1', '2','3', ...
\s $\rightarrow$ อักขระช่องว่างใดก็ได้\s $\rightarrow$ ' ', '\t', ...
metacharacters หลายตัวสามารถใส่ในวงเล็บเหลี่ยมได้:
[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$ ไม่มีอักขระ หรือซ้ำกี่ครั้งก็ได้a* $\rightarrow$ '', 'a', 'aa', ...
+ $\rightarrow$ อักขระปรากฏอย่างน้อยหนึ่งครั้ง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
ตัวอักษร ตัวเลข ขีดล่าง หรือจุดอย่างน้อยหนึ่งตัว
ตัวอย่าง:
**[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
ตัวอักษรพิมพ์เล็กอย่างน้อยหนึ่งตัว
ตัวอย่าง:
**[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
ตัวอักษรพิมพ์เล็กอย่างน้อยหนึ่งตัว
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