Python tutorial-common re module methods


Common re module methods

The table below shows some common methods of re module.

 
Method Description
compile(pattern[, flags]) Compiles a regular expression pattern, returning a pattern object.
search(pattern, string[, flags]) Scans through string looking for a match to the pattern, returning a match object, or None if no match was found.
match(pattern, string[, flags]) Tries to apply the pattern at the start of the string, returning a match object, or None if no match was found.
split(pattern, string[, maxsplit=0]) Splits the source string by the occurrences of the pattern,  returning a list containing the resulting substrings.
findall(pattern, string) Returns a list of all non-overlapping matches in the string.  If one or more groups are present in the pattern, return a  list of groups; this will be a list of tuples if the pattern  has more than one group.      Empty matches are included in the result.
sub(pat, repl, string[, count=0]) Substitutes occurrences of pat in string with repl
escape(string) Escapes all special regexp characters in string
 
Example 1: using compile() method
 
>>> import re
>>> pattern='ython'
>>> obj_pattern=re.compile(pattern)
>>> print obj_pattern
<_sre.SRE_Pattern object at 0x0187DE58>
>>> 
Note: When you supply a string pattern to methods of re module (except compile() method), the pattern is transformed in to a pattern object before the match operation occurs. With compile() method you get a pattern object already. You just supply the pattern object to the methods.
 
Example 2: using search() method
Now, we use obj_pattern produced in the above example with search() method.
>>> text='I like Python.'
>>> if re.search(obj_pattern,text):
                        print 'Matched'
else:
            print 'Not matched'
 
           
Matched
>>> 
 
Example 3: using match() method
 
>>> pat=re.compile('@')
>>> text='Dara.Yuk@gmail.com'
>>> if re.match(pat,text):
            print 'Valid email'
else:
            print 'Invalid email'
           
Invalid email
>>> 
As you already knew, match() method can only match the pattern to the beginning of a text. Therefore, the result ‘Invalid email’ is right.
 
>>> pat=re.compile('ht+p')
>>> text='http://www.worldbestlearningcenter.com'
>>> if re.match(pat,text):
            print 'Matched'
else:
            print 'Not matched'
           
Matched
>>> 
 
Example 4: using split() method
 
>>> pat=re.compile(' ')
>>> text='Hello World'
>>> x=re.split(pat,text)
>>> x[0]
'Hello'
>>> x[1]
'World'
>>> 
Note: in split() method, a pattern string or object is a separator.
 
Example 5: using findall() method
 
>>> pat=re.compile(r'\\')
>>> text='C:\Python25\Doc'
>>> re.findall(pat,text)
['\\', '\\']
>>> 
 
Example 6: using sub() method
 
>>> pat='{name}'
>>> text='Dear {name}...'
>>> re.sub(pat,'Mr.Dara',text)
'Dear Mr.Dara...'
>>> text
'Dear {name}...'
>>> 
 
Note: the sub() method will not affect the original text. It only returns a new text with the replacement.
 
Example 7: using escape() method
 
>>> re.escape('@gmail.com')
'\\@gmail\\.com'
>>> 


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.