Information for Authors resource now available
18 ปีที่ผ่านมา
import python for everyone in thailand: happycoding()
>>>import re
>>>pat = 'Hello'
>>>raw = 'Hello world. Hello you.'
>>>re.compile(pat)
>>><_sre.SRE_Pattern object at 0x01357500>
>>>re.findall(pat,raw)
>>>['Hello', 'Hello']
>>>re.finditer(pat,raw)
>>>
>>>re.match(pat,raw)
>>><_sre.SRE_Match object at 0x01FA7BF0>
>>>re.search(pat,raw)
>>><_sre.SRE_Match object at 0x01FA7BF0>
>>>re.split(pat,raw)
>>>['', ' world. ', ' you.']
>>>re.sub(pat,'Sawasdee', raw)
>>>'Sawasdee world. Sawasdee you.'
>>>re.subn(pat,'Sawasdee',raw)
>>>('Sawasdee world. Sawasdee you.', 2)
>>>re.template(pat)
>>><_sre.SRE_Pattern object at 0x013576A0>
>>> import re
>>> my_pattern = 'a{2}'
>>> raw_string = 'a aa a aa'
>>> re.sub(my_pattern,'-',raw_string)
'a - a -'
>>> my_pattern = 'a{2,4}'
>>> raw_string = 'a aa aaa aaaa aaaaa'
>>> re.sub(my_pattern,'-',raw_string)
'a - - - -a'
>>> my_pattern = '[A-Z]'
>>> raw_string = 'Apple Banana'
>>> re.sub(my_pattern,'-',raw_string)
'-pple -anana'
>>> my_pattern = '[c|b]at'
>>> raw_string = 'cat rat bat'
>>> re.sub(my_pattern,'-',raw_string)
'- rat -'
>>> my_pattern = '(ab)'
>>> raw_string = 'aa ab bb'
>>> re.sub(my_pattern,'-',raw_string)
'aa - bb'
>>> my_pattern = '(ab)+'
>>> raw_string = 'aa ab abab baba'
>>> re.sub(my_pattern,'-',raw_string)
'aa - - b-a'
>>> import re
>>> my_pattern = '.at'
>>> raw_string = 'I love rat and cat'
>>> re.sub(my_pattern,'dog',raw_string)
'I love dog and dog'
>>> my_pattern = '^dog'
>>> raw_string = 'dog dog dog'
>>> re.sub(my_pattern,'cat',raw_string)
'cat dog dog'
>>> my_pattern = 'dog$'
>>> raw_string = 'dog dog dog'
>>> re.sub(my_pattern,'cat',raw_string)
'dog dog cat'
>>> my_pattern = 'ab*'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'-- , - , - , b- , bb , bc'
>>> my_pattern = 'ab+'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'aa , - , - , ba , bb , bc'
>>> my_pattern = 'ab?'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'-- , -b , -bb , b- , bb , bc'
>>>import re
>>>my_pattern = '\AMister'
>>>raw_string = 'Mister python and Mister Thailand'
>>>re.sub(my_pattern,'MR.',raw_string)
'MR. python and Mister Thailand'
>>>import re
>>>s = '100 NORTH BROAD ROAD'
>>>re.sub(r'\bROAD\b', 'RD.', s)
'100 NORTH BROAD RD.'
>>>my_pattern = '\BROAD'
>>>raw_string = 'BROAD ROAD'
>>>re.sub(my_pattern,'RD.',raw_string)
'BRD. ROAD'
>>>my_pattern = '\d'
>>>raw_string = 'credit number 1234-1234-1234'
>>>re.sub(my_pattern,'x',raw_string)
'credit number xxxx-xxxx-xxxx'
>>>my_pattern = '\D'
>>>raw_string = 'credit number 1234-1234-1234'
>>>re.sub(my_pattern,'x',raw_string)
'xxxxxxxxxxxxxx1234x1234x1234'
>>>my_pattern = '\s'
>>>raw_string = 'a b c d e'
>>>re.sub(my_pattern,'_',raw_string)
'a_b_c_d_e'
>>>my_pattern = '\S'
>>>raw_string = 'a b c d e'
>>>re.sub(my_pattern,'_',raw_string)
'_ _ _ _ _'
>>>my_pattern = '\w'
>>>raw_string = 'a!b#c%d&e.'
>>>re.sub(my_pattern,'_',raw_string)
'_!_#_%_&_.'
>>>my_pattern = '\W'
>>>raw_string = 'a!b#c%d&e.'
>>>re.sub(my_pattern,'_',raw_string)
'a_b_c_d_e_'
>>>my_pattern = 'Mister\Z'
>>>raw_string = 'Mister and Mister'
>>>re.sub(my_pattern,'MR.',raw_string)
'Mister and MR.'
>>>import re
>>>s = '100 NORTH BROAD ROAD'
>>>re.sub(r'\bROAD\b', 'RD.', s)
'100 NORTH BROAD RD.'
>>> s = '100 NORTH MAIN ROAD'
>>> s.replace('ROAD', 'RD.')
'100 NORTH MAIN RD.'
>>> s = '100 NORTH BROAD ROAD'
>>> s.replace('ROAD', 'RD.')
'100 NORTH BRD. RD.'