i

Python Programming

Regular Expression Modifiers / Option Flags

Regular expression literals include an optional modifier to control various aspects matching. The modifiers are specified optional-flag. You can use multiple modifiers by exclusive (OR (|), as shown-previously:

Modifier & Description

re.I

Performs case/insensitive matching.

re.L

Interprets words according to current locale. This interpretation affects alphabetic group (\w and \W), as word boundary behavior(\b and \B).

re.M

Makes $ match the end of line and makes ^ match the start of any line

re.S

Period (dot) match any characterincluding the newline.

re.U

Interprets letters according to Unicode character set. This flag affects behavior of \w, \W, \b, \B.

re.X

Permits "cuter" regular expression syntax. ignores whitespace (except inside set [] or when escaped by a backslash and treats un-escaped # as comment marker.

Regular-Expression-Patterns

Except for control character (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape control character by preceding with backslash.

The following table lists regular expression syntax:

Pattern use Description          ^

Matches beginning of line       $

Matches end of line                .

Matches single character except newline      [...]

Matches single/character in brackets             [^...]

Matches single/character not in brackets       re*

Matches 0 or more occurrences in preceding expression     re+

Matches 1 or more occurrence inpreceding expression re?

Matches 0 or 1 occurrence inpreceding expression              re{ n}

Matches exactly n number of occurrences in preceding expression                        re{ n,}

Matches n or more occurrences in preceding-expression                             re{ n, m}

Matches at the least n and at most m occurrences in preceding-expression            a| b

Matches a / b              (re)

Groups regular expression used matching text          (?imx)

Toggles on i, m, or x options regular expression.

If in parentheses only that area affected        (?-imx)

Toggles off i, m, or x options in regular expression.

If in parentheses only that area affected        (?: re)

Groups regular-expressions without ponder/matched text                (?imx: re)

Temporarily-toggles on i, m, or x options in parentheses                   (?-imx: re)

Temporarily-toggles off i, m, or x optionin parentheses                     (?#...)

Regular-Expression-Examples

Literal-characters

Example & Description

python

Match "python".

Character-classes

Example & Description

[Pp]ython

Match "Python" or "python"

rub[ye]

"ruby" or "rube"

[aeiou]

Match any lowercase vowel

[0-9]

Match any digit; 0123456789]

[a-z]

Match any lowercase ASCI-letter

[A-Z]

Match any uppercase ASCII-letter

[a-zA-Z0-9]

Match any of above

[^aeiou]

Match anything other thanlowercase vowel

[^0-9]

Match anything than a digit