Python - String Pattern Matching


Python string supports pattern-based text processing.

Python module re has calls for searching, splitting, and replacement:


import re 
match = re.match('Hello[ \t]*(.*)world', 'Hello    Python world') 
print( )


Here, we search for a substring that

  • begins with the word "Hello,"
  • followed by zero or more tabs or spaces,
  • followed by arbitrary characters to be saved as a matched group,
  • terminated by the word "world."

If such a substring is found, portions of the substring matched by parts of the pattern are available as groups.

The following pattern picks out three groups separated by slashes, and is similar to splitting by an alternatives pattern:


import re 
match = re.match('[/:](.*)[/:](.*)[/:](.*)', '/usr/home:lumberjack') 
print( match.groups() )
print( re.split('[/:]', '/usr/home/lumberjack') )


Related Topic