9.3. Syntax Anchor¶
.
- any character except a newline (changes meaning withre.DOTALL
)^
- start of line (changes meaning withre.MULTILINE
)$
- end of line (changes meaning withre.MULTILINE
)\A
- start of text (doesn't change meaning withre.MULTILINE
)\Z
- end of text (doesn't change meaning withre.MULTILINE
)
9.3.1. Any Character¶
.
- any character except a newline (changes meaning withre.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(r'No.', TEXT)
['Nov']
Search for uppercase letter followed by any three characters:
>>> re.findall(r'[A-Z]...', TEXT)
['Mark', 'Watn', 'Ares', 'Mars', 'Nov ']
Example:
>>> re.findall(r'1:37 ..', TEXT)
['1:37 pm']
>>>
>>> re.findall(r'Nov 7..', TEXT)
['Nov 7th']
9.3.2. Start of Line¶
^
- start of a lineChanges 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(r'^[A-Z]', TEXT)
['M']
Search for a capital letter anywhere in text:
>>> re.findall(r'[A-Z]', TEXT)
['M', 'W', 'A', 'M', 'N']
9.3.3. End of Line¶
$
- end of lineChanges 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(r'..$', TEXT)
['pm']
9.3.4. Start of String¶
\A
- start of a textDoesn'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(r'\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(r'^[A-Z]', TEXT)
['M']
9.3.5. End of String¶
\Z
- end of a textDoesn'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(r'..\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(r'..$', TEXT)
['pm']
9.3.6. Use Case - 0x01¶
abc.e
- text abc then any character followed by letter e
9.3.7. Assignments¶
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Use regular expressions find in text:
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. Użyj wyrażeń regularnych wyszukiwania w tekście:
a. wielkie litery na początku tekstu
b. wielkie litery na początku każdej linii
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True)
['A']
>>> pprint(result_b, compact=True)
['A', 'J', 'T', 'B', '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 uppercase letter at the beginning of a text
# Example: 'A'
# type: list[str]
result_a = ...
# Find uppercase letter at the beginning of each line
# Example: 'A', 'J', 'T', 'B', 'M', 'C'
# type: list[str]
result_b = ...
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Use regular expressions find in text:
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. Użyj wyrażeń regularnych wyszukiwania w tekście:
a. dowolny znak na początku tekstu
b. dowolny znak na początku każdej linii
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True)
['A']
>>> pprint(result_b, compact=True)
['A', 'h', 'p', 'J', 't', 'o', 'T', 'B', 'o', 'f', '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 any character at the beginning of a text
# Example: 'A'
# type: list[str]
result_a = ...
# Find any character at the beginning of each line
# Example: 'A', 'h', 'p', 'J', 't', 'o', 'T', 'B', 'o', 'f', 'M', 'C'
# type: list[str]
result_b = ...
"""
* Assignment: RE Syntax Anchor
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Use regular expressions find in text:
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. Użyj wyrażeń regularnych wyszukiwania w tekście:
a. dowolny znak na końcu tekstu
b. dowolny znak na końcu każdej linii
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True)
['.']
>>> pprint(result_b, compact=True)
['d', 'e', 'n', 'n', ',', '.', 'y', ')', ')', '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 any character at the end of a text
# Example: '.'
# type: list[str]
result_a = ...
# Find any character at the end of each line
# Example: 'd', 'e', 'n', 'n', ',', '.', 'y', ')', ')', 'e', 'n', '.'
# type: list[str]
result_b = ...