4.3. Regex 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)

4.3.1. SetUp

>>> import re

4.3.2. Any Character

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

Search for letters No followed by any character:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'.st', TEXT)
['1st']

Search for uppercase letter followed by any three characters:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'[A-Z]..', TEXT)
['Ema', 'Mar', 'Wat', 'Sat', 'Jan']

Example:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'Jan 1..', TEXT)
['Jan 1st']

4.3.3. Start of Line

  • ^ - start of a line

  • Changes meaning with re.MULTILINE

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

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'^[A-Z]', TEXT)
['E']

Search for a capital letter anywhere in text:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'[A-Z]', TEXT)
['E', 'M', 'W', 'S', 'J', 'A', 'M']

4.3.4. End of Line

  • $ - end of line

  • Changes meaning with re.MULTILINE

Give me last characters in a line:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'.$', TEXT)
['M']

4.3.5. Start of String

  • \A - start of a text

  • Doesn't change meaning with re.MULTILINE

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

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'\A[A-Z]', TEXT)
['E']

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

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'^[A-Z]', TEXT)
['E']

4.3.6. End of String

  • \Z - end of a text

  • Doesn't change meaning with re.MULTILINE

Give me last character in a text:

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'.\Z', TEXT)
['M']

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

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'.$', TEXT)
['M']

4.3.7. Use Case - 0x01

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

4.3.8. Assignments

Code 4.6. Solution
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define `result: str` with regular expression to find:
        a. uppercase letter at the beginning of a text
        b. uppercase letter at the beginning of each line
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
        a. wielkie litery na początku tekstu
        b. wielkie litery na początku każdej linii
    2. Uruchom doctesty - wszystkie muszą się powieść

References:
    [1] Authors: Wikipedia contributors
        Title: Apollo 11
        Publisher: Wikipedia
        Year: 2019
        Retrieved: 2019-12-14
        URL: https://en.wikipedia.org/wiki/Apollo_11

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> result = re.findall(result_a, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['A']

    >>> result = re.findall(result_b, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['A', 'J', 'T', 'T', 'M', 'C']
"""

import re

TEXT = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named
Tranquility Base upon landing. Armstrong and Aldrin collected 47.5 pounds
(21.5 kg) of lunar material to bring back to Earth as pilot Michael Collins
(CMP) flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""

# Find all uppercase letter at the beginning of a text
# Example: 'A'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''

# Find all uppercase letter at the beginning of each line
# Example: 'A', 'J', 'T', 'B', 'M', 'C'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''

Code 4.7. Solution
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define `result: str` with regular expression to find:
        a. any character at the beginning of a text
        b. any character at the beginning of each line
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
        a. dowolny znak na początku tekstu
        b. dowolny znak na początku każdej linii
    2. Uruchom doctesty - wszystkie muszą się powieść

References:
    [1] Authors: Wikipedia contributors
        Title: Apollo 11
        Publisher: Wikipedia
        Year: 2019
        Retrieved: 2019-12-14
        URL: https://en.wikipedia.org/wiki/Apollo_11

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> result = re.findall(result_a, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['A']

    >>> result = re.findall(result_b, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['A', 'h', 'p', 'J', 't', 'o', 'T', 'T', '(', '(', 'M', 'C']
"""

import re

TEXT = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named
Tranquility Base upon landing. Armstrong and Aldrin collected 47.5 pounds
(21.5 kg) of lunar material to bring back to Earth as pilot Michael Collins
(CMP) flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""

# Find character at the beginning of a text
# Example: 'A'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''

# Find characters at the beginning of each line
# Example: 'A', 'h', 'p', 'J', 't', 'o', 'T', 'B', 'o', 'f', 'M', 'C'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''

Code 4.8. Solution
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define `result: str` with regular expression to find:
        a. any character at the end of a text
        b. any character at the end of each line
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
        a. dowolny znak na końcu tekstu
        b. dowolny znak na końcu każdej linii
    2. Uruchom doctesty - wszystkie muszą się powieść

References:
    [1] Authors: Wikipedia contributors
        Title: Apollo 11
        Publisher: Wikipedia
        Year: 2019
        Retrieved: 2019-12-14
        URL: https://en.wikipedia.org/wiki/Apollo_11

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> result = re.findall(result_a, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['.']

    >>> result = re.findall(result_b, TEXT, flags=re.MULTILINE)
    >>> pprint(result, compact=True)
    ['d', 'e', 'n', 'n', ',', '.', 'd', 's', 's', 'e', 'n', '.']
"""

import re

TEXT = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named
Tranquility Base upon landing. Armstrong and Aldrin collected 47.5 pounds
(21.5 kg) of lunar material to bring back to Earth as pilot Michael Collins
(CMP) flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""

# Find character at the end of a text
# Example: '.'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''

# Find characters at the end of each line
# Example: 'd', 'e', 'n', 'n', ',', '.', 'y', ')', ')', 'e', 'n', '.'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''