Python - String Pattern Matching

Introduction

Python string supports pattern-based text processing.

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

Demo

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

Result

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:

Demo

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

Result

Related Topic