8.3. Syntax Anchor

  • . - any character except a newline (changes meaning with re.DOTALL)

  • ^ - start of line (changes meaning with re.MULTILINE)

  • $ - end of line (changes meaning with re.MULTILINE)

  • \A - start of text (doesn't change meaning with re.MULTILINE)

  • \Z - end of text (doesn't change meaning with re.MULTILINE)

8.3.1. Any Character

  • . - any character except a newline (changes meaning with re.DOTALL)

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

Search for letters No followed by any character:

>>> re.findall('No.', TEXT)
['Nov']

Search for uppercase letter followed by any three characters:

>>> re.findall('[A-Z]...', TEXT)
['Mark', 'Watn', 'Ares', 'Mars', 'Nov ']

Example:

>>> re.findall('1:37 ..', TEXT)
['1:37 pm']
>>>
>>> re.findall('Nov 7..', TEXT)
['Nov 7th']

8.3.2. Start of Line

  • ^ - start of a line

  • Changes meaning with re.MULTILINE

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

Search for a capital letter in text at the start of a line:

>>> re.findall('^[A-Z]', TEXT)
['M']

Search for a capital letter anywhere in text:

>>> re.findall('[A-Z]', TEXT)
['M', 'W', 'A', 'M', 'N']

8.3.3. End of Line

  • $ - end of line

  • Changes meaning with re.MULTILINE

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

Give me last two characters in a text:

>>> re.findall('..$', TEXT)
['pm']

8.3.4. Start of String

  • \A - start of a text

  • Doesn't change meaning with re.MULTILINE

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

Search for a capital letter in text at the start of a line:

>>> re.findall('\A[A-Z]', TEXT)
['M']

Note, that the output is identical to Start of a Line ^. It will differ when re.MULTILINE flag is present.

>>> re.findall('^[A-Z]', TEXT)
['M']

8.3.5. End of String

  • \Z - end of a text

  • Doesn't change meaning with re.MULTILINE

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

Give me last two characters in a text:

>>> re.findall('..\Z', TEXT)
['pm']

Note, that the output is identical to Start of a Line ^. It will differ when re.MULTILINE flag is present.

>>> re.findall('..$', TEXT)
['pm']

8.3.6. Use Case - 0x01

  • abc.e - text abc then any character followed by letter e