Python Regular Expression 2, python Regular Expression
Matching phone number:
1 >>> import re 2 >>> pattern=r'\d{3,4}-?\d{8}' 3 >>> re.findall( pattern, '021-12345678' ) 4 ['021-12345678'] 5 >>> re.findall( pattern, '02188888888' ) 6 ['02188888888'] 7 >>> re.findall( pattern, '0218888888' ) 8 [] 9 >>> re.findall( pattern, '021-8888888' )10 []11 >>>
You can program regular expressions into an object to improve matching efficiency.
>>> patterTel=re.compile( pattern )>>> patterTel<_sre.SRE_Pattern object at 0x7fd19ba5ab90>>>> patterTel.findall( '021-88888888' )['021-88888888']>>>
You can compile it into an object, pass the parameter (re. I), and change the matching method to ignore the size.
1 >>> pattern=r'[a-z]+' 2 >>> re.findall( pattern, 'abc' ) 3 ['abc'] 4 >>> re.findall( pattern, 'aBc' ) 5 ['a', 'c'] 6 >>> re.compile( pattern, re.I ) 7 <_sre.SRE_Pattern object at 0x7fd19b906490> 8 >>> cPattern = re.compile( pattern, re.I ) 9 >>> cPattern.findall( 'aBc' )10 ['aBc']11 >>>
Match: whether the matched character starts with the matched string. If yes, an object is returned.
>>> Pattern = r'abc' >>> re. match (pattern, 'ghostwu abc')> re. match (pattern, 'abc ghostw') <_ sre. SRE_Match object at 0x7fd19b96c780 >>> re. match (pattern, 'abc ghostwu')> res = re. match (pattern, 'abc ghostwu') >>> if res :... print 'started with abc '... else :... print 'not starting with abc '... starting with abc>
Search: returns an object if it exists.
>>> pattern=r'abc'>>> re.search( pattern, 'abc ghostwu' )<_sre.SRE_Match object at 0x7fd19b96c850>>>> re.search( pattern, 'ghostwu abc' )<_sre.SRE_Match object at 0x7fd19b96c780>>>> re.search( pattern, 'ghostwu abc def' )<_sre.SRE_Match object at 0x7fd19b96c850>>>> re.search( pattern, 'ghostwu def' )>>>
Finditer returns an iterator, and findall returns a list
>>> pattern=r'abc'>>> iterator=re.finditer( pattern, 'abc def abc ghostwu abc' )>>> iterator.next()<_sre.SRE_Match object at 0x7fd19b96c780>>>> iterator.next()<_sre.SRE_Match object at 0x7fd19b96c850>>>> iterator.next()<_sre.SRE_Match object at 0x7fd19b96c780>>>> iterator.next()Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration>>>
The group method to view the matching values of the match.
>>> pattern=r'abc'>>> res=re.match( pattern, 'abc ghostwu abc' )>>> res<_sre.SRE_Match object at 0x7fd19b96c850>>>> res.group()'abc'
Objects returned by the iterator
>>> pattern=r'abc'>>> re.finditer( pattern, 'abc ghostwu abc abc hello' )<callable-iterator object at 0x7fd19b982190>>>> res = re.finditer( pattern, 'abc ghostwu abc abc hello' )>>> res.next().group()'abc'>>> res.next().group()'abc'>>> res.next().group()'abc'>>> res.next().group()Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration>>>
Re. sub: replace
>>> str='hello abc'>>> str.replace( 'abc', 'ghostwu' )'hello ghostwu'>>> str'hello abc'>>> re.sub( r'abc', 'ghostwu', str )'hello ghostwu'>>>
Replace lowercase letters (r, s, t) with uppercase letters (R, s, t)
>>> str='ghostwu:hi,nice to meet you!'>>> re.sub( r'[r-t]', 'R', str )'ghoRRwu:hi,nice Ro meeR you!'>>>
>>> str=r'jfsdaft jdfasfcxvt jfdsafdast'>>> pattern=r'j.*t?'>>> re.sub( pattern, 'javascript', str )'javascript'>>> re.subn( pattern, 'javascript', str )('javascript', 1)
Subn: a number is added to the result, indicating the total number of replications
Split: Cut
>>> ip='127.0.0.1'>>> ip.split( '.' )['127', '0', '0', '1']>>> pattern='\.'>>> re.split( pattern, ip )['127', '0', '0', '1']
>>> str="hello, my name is ghostwu">>> pattern=r'[\s,]'>>> re.split( pattern, str )['hello', '', 'my', 'name', 'is', 'ghostwu']>>>
If the form of the Delimiter is different, it is more convenient to use the regular split.