8.2. Syntax Qualifier

  • Qualifier specifies what to find.

  • a - Exact

  • a|b - Alternative

  • [abc] - Enumeration

  • [a-z] - Range

8.2.1. SetUp

>>> import re

8.2.2. Exact

  • a - Exact

>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Regular expressions allows to find exact matches:

>>> re.findall('a', TEXT)
['a', 'a', 'a', 'a', 'a']

Note, that regular expressions are case sensitive (unless re.IGNORECASE flag is present. More information in Syntax Flags):

>>> re.findall('A', TEXT)
['A']

Note, that regular expressions are used to search in text, therefore in case of searching for a number it will return a strings with numbers in it:

>>> re.findall('1', TEXT)
['1']

Python re.findall() function will return empty list if none match was found:

>>> re.findall('x', TEXT)
[]

8.2.3. Exact Alternate

  • a|b - letter a or b (also works with expressions)

>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Alternative allows to search for two or more possible matches:

>>> re.findall('a|b', TEXT)
['a', 'a', 'a', 'a', 'a']

It can find more than two matches:

>>> re.findall('a|b|c|d', TEXT)
['a', 'a', 'a', 'd', 'd', 'a', 'a']
>>>
>>> re.findall('1|2|3', TEXT)
['3', '2', '3', '1', '3']

It will work for both numbers, characters or any other object:

>>> re.findall('a|b|c|d|1|2|3', TEXT)
['a', 'a', '3', 'a', 'd', 'd', 'a', '2', '3', 'a', '1', '3']

Examples:

>>> re.findall('a|e', TEXT)
['a', 'a', 'e', 'e', 'a', 'e', 'a', 'a']
>>>
>>> re.findall('a|e|i|o|u|y', TEXT)
['a', 'a', 'e', 'y', 'o', 'e', 'a', 'e', 'o', 'a', 'o', 'o', 'a']

8.2.4. Enumeration

  • [abc] - letter a or b or c

>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Enumerations provide compact and more readable syntax for longer alternatives:

>>> re.findall('[abcd]', TEXT)
['a', 'a', 'a', 'd', 'd', 'a', 'a']
>>>
>>> re.findall('[123]', TEXT)
['3', '2', '3', '1', '3']

It will work for both numbers, characters or any other object:

>>> re.findall('[abcd123]', TEXT)
['a', 'a', '3', 'a', 'd', 'd', 'a', '2', '3', 'a', '1', '3']

Examples:

>>> re.findall('[a-z]', TEXT)  
['a', 'r', 'k', 'a', 't', 'n', 'e', 'y', 'o', 'f', 'r', 'e', 's', 'l',
 'a', 'n', 'd', 'e', 'd', 'o', 'n', 'a', 'r', 's', 'o', 'n', 'o', 'v',
 't', 'h', 'a', 't', 'p', 'm']
>>>
>>> re.findall('[az-]', TEXT)  
['a', 'a', 'a', 'a', 'a']
>>> re.findall('[A-z]', TEXT)  
['M', 'a', 'r', 'k', 'W', 'a', 't', 'n', 'e', 'y', 'o', 'f', 'A', 'r',
 'e', 's', 'l', 'a', 'n', 'd', 'e', 'd', 'o', 'n', 'M', 'a', 'r', 's',
 'o', 'n', 'N', 'o', 'v', 't', 'h', 'a', 't', 'p', 'm']
>>>
>>> re.findall('[a-Z]', TEXT)
Traceback (most recent call last):
re.error: bad character range a-Z at position 1
>>>
>>> re.findall('[z-a]', TEXT)
Traceback (most recent call last):
re.error: bad character range z-a at position 1

Use Cases:

>>> re.findall('[aeiouy]', TEXT)
['a', 'a', 'e', 'y', 'o', 'e', 'a', 'e', 'o', 'a', 'o', 'o', 'a']
>>>
>>> re.findall('a|e|i|o|u|y', TEXT)
['a', 'a', 'e', 'y', 'o', 'e', 'a', 'e', 'o', 'a', 'o', 'o', 'a']

8.2.5. Range

  • [a-z] - any lowercase ASCII letter from a to z

  • [A-Z] - any uppercase ASCII letter from A to Z

  • [0-9] - any digit from 0 to 9

  • [a-zA-Z] - any ASCII letter from: a to z or from A to Z

  • [A-z] - any ASCII letter from: a to z or from A to Z

  • [a-zA-Z0-9] - any ASCII letter from a to z or from A to Z or digit from 0 to 9

>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Ranges provide even more readable and convenient way os specifying particular characters to match. It is very useful to define ranges of numbers or letters this way:

>>> re.findall('[a-z]', TEXT)  
['a', 'r', 'k', 'a', 't', 'n', 'e', 'y', 'o', 'f', 'r', 'e', 's', 'l',
 'a', 'n', 'd', 'e', 'd', 'o', 'n', 'a', 'r', 's', 'o', 'n', 'o', 'v',
 't', 'h', 'a', 't', 'p', 'm']
>>>
>>> re.findall('[A-Z]', TEXT)
['M', 'W', 'A', 'M', 'N']
>>>
>>> re.findall('[0-9]', TEXT)
['3', '7', '2', '0', '3', '5', '1', '3', '7']

Note, that regular expressions are case sensitive (unless re.IGNORECASE flag is present. More information in Syntax Flags). You can also join ranges to create even broader matches:

>>> re.findall('[a-zA-Z]', TEXT)  
['M', 'a', 'r', 'k', 'W', 'a', 't', 'n', 'e', 'y', 'o', 'f', 'A', 'r',
 'e', 's', 'l', 'a', 'n', 'd', 'e', 'd', 'o', 'n', 'M', 'a', 'r', 's',
 'o', 'n', 'N', 'o', 'v', 't', 'h', 'a', 't', 'p', 'm']
>>>
>>> re.findall('[a-zA-Z0-9]', TEXT)  
['M', 'a', 'r', 'k', 'W', 'a', 't', 'n', 'e', 'y', 'o', 'f', 'A', 'r',
 'e', 's', '3', 'l', 'a', 'n', 'd', 'e', 'd', 'o', 'n', 'M', 'a', 'r',
 's', 'o', 'n', 'N', 'o', 'v', '7', 't', 'h', '2', '0', '3', '5', 'a',
 't', '1', '3', '7', 'p', 'm']

Ranges are ordered in ASCII table order (more information in Locale Encoding). Because uppercase letters are before lowercase letters (has lower indexes), you can define range from Z-a, but the opposite is not true:

>>> re.findall('[Z-a]', TEXT)
['a', 'a', 'a', 'a', 'a']
>>> re.findall('[a-Z]', TEXT)
Traceback (most recent call last):
re.error: bad character range a-Z at position 1

The last example can work in some other languages due to the different implementation of the algorithm or PCRE standard. More information in Syntax Extensions.

Mind that ranges not necessarily need to be from a-z. It could be any alphabetic or numeric range:

>>> re.findall('[3-7]', TEXT)
['3', '7', '3', '5', '3', '7']
>>>
>>> re.findall('[C-Y]', TEXT)
['M', 'W', 'M', 'N']
>>>
>>> re.findall('[3-7C-Y]', TEXT)
['M', 'W', '3', 'M', 'N', '7', '3', '5', '3', '7']

8.2.6. Joining

  • [abc]|[123] - Enumeration alternative - letter a, b or c or digit 1, 2 3

  • [a-z]|[0-9] - Range alternative - any lowercase ASCII letter from a to z or digit from 0 to 9

>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Alternative enumerations syntax is as follows:

>>> re.findall('[abc]|[123]', TEXT)
['a', 'a', '3', 'a', 'a', '2', '3', 'a', '1', '3']

The effect is identical to:

>>> re.findall('[abc123]', TEXT)
['a', 'a', '3', 'a', 'a', '2', '3', 'a', '1', '3']

You can define alternative ranges to find:

>>> re.findall('[A-Z]|[0-9]', TEXT)
['M', 'W', 'A', '3', 'M', 'N', '7', '2', '0', '3', '5', '1', '3', '7']

The effect is identical to:

>>> re.findall('[A-Z0-9]', TEXT)
['M', 'W', 'A', '3', 'M', 'N', '7', '2', '0', '3', '5', '1', '3', '7']

8.2.7. Examples

  • [d-m] - any lowercase letter from d to m

  • [3-7] - any digit from 3 to 7

  • [xz2] - x or z or 2

  • [d-mK-P3-8] - any lowercase letter from d to m or uppercase letter from K to P or digit from 3 to 8

  • x|z|2 - x or z or 2

  • d|x - d or x

  • [d-k]|[ABC]|[3-8] - any lowercase letter from d to k or uppercase A,`B` or C or digit from 3 to 8

8.2.8. Use Case - 0x01

>>> import re
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'

Note, the nd in word landed:

>>> re.findall('st|nd|rd|th', TEXT)
['nd', 'th']
>>> re.findall('[st|nd|rd|th]', TEXT)
['r', 't', 'n', 'r', 's', 'n', 'd', 'd', 'n', 'r', 's', 'n', 't', 'h', 't']
>>> re.findall('[stndrdth]', TEXT)
['r', 't', 'n', 'r', 's', 'n', 'd', 'd', 'n', 'r', 's', 'n', 't', 'h', 't']