1 / 5

Perl Regular Expressions – Part 1

Perl Regular Expressions – Part 1. Justin Hummel CS 265. Basic Matching. Simple Matching “Hello World” =~ /World/ # matches Matching a variable “Hello World” =~ /$greeting/ Matching the default variable ($_) /World/ Arbitrary delimiters “Hello World” =~ m”World”. Basic Matching.

Télécharger la présentation

Perl Regular Expressions – Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Regular Expressions – Part 1 Justin Hummel CS 265

  2. Basic Matching • Simple Matching • “Hello World” =~ /World/ # matches • Matching a variable • “Hello World” =~ /$greeting/ • Matching the default variable ($_) • /World/ • Arbitrary delimiters • “Hello World” =~ m”World”

  3. Basic Matching • Metacharacters – need to be escaped with “\” • {}[]()^$.|*+?\ • /$5/ # does not match “$5” • /\$5/ # matches “5” • Special Escape Sequences • \n (new line) \t (tab) \r (carriage return) \a (bell) • Anchor Metacharacters • ^ (Beginning) $ (End) • /^cat/ # matches “cat” and “catfish”, not “bobcat” • /cat$/ # matches “cat” and “bobcat”, not “catfish”

  4. Character Classes • […] denotes a character class • Will match any of the characters • /[bcr]at/ # matches “bat” “cat” or “rat” • “–” Character creates a range • /[0-9]/ # matches 0,1,2,3,4,5,6,7,8,9 • “^” Character negates the character class • /[^0-9]/ # matches anything but 0,1,2,3,4,5,6,7,8,9 • Special Character Classes • \d (digit) \s (whitespace) \w (word character) . (anything but \n) • Negated Special Character Classes • Capitalized (\D,\S,\W)

  5. Modifiers • “s” – treat the entire string as a single line • “.” matches “\n” • “m” – treat the string as a set of multiple lines • “.” does not match “\n” • “^” and “$” match the start and end of the string • “sm” – treat the string as a single long line, but detect multiple lines • “.” does not match “\n” • “^” and “$” match the start/end of any line in the string

More Related