50 likes | 67 Views
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.
E N D
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 • 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”
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)
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