Berlatih Pertanyaan Wawancara Coding di Python
Kirill Smirnov
Data Science Consultant, Altran
Regular expression - rangkaian karakter khusus (metakarakter) yang mendefinisikan pola pencarian dalam teks.
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
Regular expression - rangkaian karakter khusus (metakarakter) yang mendefinisikan pola pencarian dalam teks.
cat
"I have a cat. My cat likes to eat a lot. It also catches mice."
Contoh:
[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected].
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
Karakter dan angka biasa dipetakan ke dirinya sendiri:
a $\rightarrow$ aA $\rightarrow$ A1 $\rightarrow$ 1Titik memetakan ke apa saja:
. $\rightarrow$ karakter apa pun. $\rightarrow$ 'a', '1', '"', ' ', ...
\. $\rightarrow$ .Metakarakter berikut merepresentasikan \ diikuti huruf:
\w $\rightarrow$ karakter alfanumerik atau underscore\w $\rightarrow$ '1', 'a','_', ...
\d $\rightarrow$ digit apa pun\d $\rightarrow$ '1', '2','3', ...
\s $\rightarrow$ karakter spasi putih\s $\rightarrow$ ' ', '\t', ...
Beberapa metakarakter bisa diapit tanda kurung siku:
[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$ tidak ada karakter atau berulang tak terbatasa* $\rightarrow$ '', 'a', 'aa', ...
+ $\rightarrow$ muncul minimal sekalia+ $\rightarrow$ 'a', 'aa', 'aaa', ...
? $\rightarrow$ ada atau tidak adaa? $\rightarrow$ '', 'a'
{n, m} $\rightarrow$ muncul dari n hingga m kalia{2, 4} $\rightarrow$ 'aa', 'aaa', 'aaaa'
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[\w\.]+' $\rightarrow$john.smith,boss,info`
minimal satu huruf, digit, underscore, atau titik
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
@ $\rightarrow$ @
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ mailbox, company
minimal satu huruf kecil
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
\. $\rightarrow$ .
Contoh:
**[email protected] adalah email John. Ia sering menulis ke bosnya di [email protected]. Namun pesannya diteruskan ke sekretarisnya di [email protected]**.
[\w\.]+@[a-z]+\.[a-z]+
[a-z]+ $\rightarrow$ com
minimal satu huruf kecil
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 ',
'.']
Berlatih Pertanyaan Wawancara Coding di Python