O'Reilly logo

Reguläre Ausdrücke kurz & gut, 2nd Edition by Tony Stubblebine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beispiele

#Suche nach Spider-Man, Spiderman, SPIDER-MAN usw.
import re

dailybugle = 'Spider-Man Menaces City!'
pattern    = r'spider[- ]?man.'

if re.match(pattern, dailybugle, re.IGNORECASE):
    print dailybugle
Listing 1.17 Einfaches Matching
#Datumsangaben in Formaten wie MM/DD/YYYY,
# MM-DD-YY ... erkennen
import re

date = '12/30/1969'

regex = re.compile(r'^(\d\d)[-/](\d\d)[-/](\d\d(?:\d\d)?)$')

match = regex.match(date)

if match:
    monat = match.group(1) #12
    tag   = match.group(2) #30
    jahr  = match.group(3) #1969
Listing 1.18 Matching- und Capture-Gruppe
#<br> XHTML-konform in <br /> umwandeln
import re

text  = 'Hallo Welt! <br>'
regex = re.compile(r'<br>', re.IGNORECASE);
repl  = r'<br />'

result = regex.sub(repl,text)
Listing 1.19 Einfache Substitution
#urlify ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required