Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Regular Expressions – Part 1

Similar presentations


Presentation on theme: "Perl Regular Expressions – Part 1"— Presentation transcript:

1 Perl Regular Expressions – Part 1
Justin Hummel CS 265

2 Basic Matching Simple Matching Matching a variable
“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 “^” and “$” match the start/end of any line in the string


Download ppt "Perl Regular Expressions – Part 1"

Similar presentations


Ads by Google