Advanced Search Function 1/2
The Search function also provides some advanced matching combinations. We will now introduce the most famous one and later practice them in the exercise:
\A
- Matches if the specified characters are at the start of a string;\b
- Matches if the specified characters are at the beginning or end of a word;\D
- Matches any non-decimal digit. Equivalent to [^0-9];\d
- Matches any decimal digit. Equivalent to [0-9];\B
- Opposite of\b
. Matches if the specified characters are not at the beginning or end of a word;.
- period matches any single character.
Oppgave
Swipe to start coding
- Import
re
. - Define a text variable as
"The cat in the hat"
. - Search for any 3 characters finishing with
"at"
. - Print the output.
Løsning
9
1
2
3
4
5
6
import re
# Search for any three characters in the text "The cat in the hat"
text = "The cat in the hat"
x = re.search(".at", text)
print(x) # Output: <re.Match object; span=(4, 7), match='cat'>
Mark tasks as Completed
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 3
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår