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.

dailybugle = 'Spider-Man Menaces City!'

if dailybugle.match(/spider[- ]?man./i)
    puts dailybugle
end
Listing 1.21 Einfaches Matching
#Datumsangaben in Formaten wie MM/DD/YYYY, MM-DD-YY, ...

date = '12/30/1969'

regexp = Regexp.new('^(\d\d)[-/](\d\d)[-/](\d\d(?:\d\d)?)$')

if md = regexp.match(date)
  month = md[1] #12
  day   = md[2] #30
  year  = md[3] #1969
end
Listing 1.22 Matching- und Capture-Gruppen
#<br> XHTML-konform in <br /> umwandeln

text  = 'Hello Welt. <br>'
regexp = Regexp.new('<br>', Regexp::IGNORECASE)

result = text.sub(regexp, "<br />")
Listing 1.23 Einfache Substitution
#urlify - URLs in HTML umwandeln text = 'Besuchen Sie die Website ' \ 'http://www.oreilly.de/catalog/regexpprger/.' regexp ...

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