Skip to content
Search

Python:re

lastmod 2022-05-25created 2015-03-1019 edits

Compile

Regular expressions can be compiled as below. Once compiled, you do not have to type the regular expression every time.

import re
p = re.compile('ab*')

For reference, case-insensitive matching is done as below.

import re
p = re.compile('ab*', re.IGNORECASE)

Repetition metacharacters

Metacharacter

Meaning

*

Repeat 0 or more times

+

Repeat 1 or more times

?

0 or 1 time

{m}

Repeat m times

{m, n}

Repeat m to n times

Matching metacharacters

Metacharacter

Meaning

.

Matches any character except a newline

^

Matches the start of the string

$

Matches the end of the string

[ ]

One character from a character set

|

Means "or"

{ }

Groups a regular expression

Escape sequences

Type

Description

\\

The backslash character itself

\d

Matches any digit [0-9]

\D

Matches any non-digit character [^0-9]

\s

Matches whitespace characters [ \t\n\r\f\v]

\S

Matches anything that is not whitespace [^ \t\n\r\f\v]

\w

Matches digits or letters [a-zA-Z0-9_]

\W

Matches anything that is not a digit or letter [^a-zA-Z0-9_]

\b

Word boundary. A word is a consecutive string of letters or digits

\B

Not a word boundary

\A

Matches only at the start of the string

\Z

Matches only at the end of the string

Regular expressions for minimal matching

Symbol

Meaning

*?

Same as * but matches the string minimally

+?

Same as + but matches the string minimally

??

Same as ? but matches the string minimally

{m,n}?

Same as {m,n} but matches the string minimally

Flags available in regular expressions

Flag

Description

I, IGNORECATE

Case-insensitive

L, LOCATE

Makes \w, \W, \b, \B depend on the current locale

M, MULTILINE

^ matches the very start of the string and the start of each line. $ matches the very end of the string and the end of each line.

S, DOTALL

Makes . match newline characters as well

U, UNICODE

Makes \w, \W, \b, \B depend on Unicode character properties

X, VERBOSE

Whitespace inside the regular expression is ignored

Main methods of the re module

Method

Description

compile(pattern[, flags])

Compiles pattern and returns a regular expression object

match(pattern, string[,flags])

Checks whether pattern exists from the start of string and returns a MatchObject instance

search(pattern, string[,flags])

Checks whether pattern exists anywhere in string and returns a MatchObject instance

split(pattern, string[, maxplit=0])

Splits string using pattern as the delimiter and returns a list

findall(pattern, string[, flags])

Returns the strings in string that satisfy pattern as a list

finditer(pattern, string[, flags])

Returns the strings in string that satisfy pattern as an iterator

sub(pattern, repl, string[, count=0])

Replaces the parts of string that match pattern with repl and returns the resulting string

subn(pattern, repl, string[, count=0])

Same as sub, but returns the result as a tuple (result string, number of matches)

escape(string)

Returns the string with non-alphanumeric characters backslash-escaped. (Useful when using an arbitrary string as a regular expression pattern)

Match object

A Match object is created as the result of match() or search(), and provides features for processing the search results efficiently.

Method

Attribute

group([group1, ...])

Returns the subset of the matched string result corresponding to the given index. If the index is '0' or not given, returns the entire matched string.

groups()

Returns the matched results as a tuple

groupdict()

Returns named match results as a dictionary

start([group])

Returns the start index of the matched result string. (If a subset number or given name is passed as an argument, returns the corresponding start index)

end([group])

Returns the end index of the matched result string. (If a subset number or given name is passed as an argument, returns the corresponding end index)

pos

The position in the original string where the search starts.

endpos

The position in the original string where the search ends.

lastindex

Returns the last index number in the matched result set. (Returns None if there is no match)

lastgroup

Returns the name of the last match in the matched result set. (Returns None if the regular expression's match condition has no name or there is no match)

string

The original string that is the target of matching.

  • re.match(): searches from the start of the target string and returns the result.
  • re.search(): searches the entire target string and returns the result.
  • re.findall(): returns all strings (substrings) that match the regular expression as a list.
  • re.finditer(): returns all strings (substrings) that match the regular expression as an iterator object.

Match object

The Match object returned as the result of match, search, etc. holds the following information.

  • group(): returns the matched string.
  • start(): returns the start position of the matched string.
  • end(): returns the end position of the matched string.
  • span(): returns a tuple of the (start, end) of the matched string.

Example

Properties dictionary

Assume there are properties in the following format.

TEST=
PATH=NANA

They can be read as below.

import re
def readProperties():
    with open(PROPERTIES_NAME, 'rb') as f:
        result = {}
        for line in f:
            name  = str(re.match(r'^.*=', line).group()).strip()
            value = str(line[len(name):]).strip()
            name  = str(name[:-1]).strip()
            result[name] = value
    return result

Substitution

You can substitute by group number with arguments like \1.

import re
result = re.sub(r"(\d.*?)\s(\d.*?)", r"\1 \2", string1)

See also

Favorite site

References


  1. System_Section-Python-Regular_Expression_module.pdf ↩

author Zer0 sitemap