0% found this document useful (0 votes)
18 views1 page

Beginner's Introduction To Perl - Part 3: Simple Matching

Perl regular expressions (regexes) allow powerful string manipulation. This document introduces simple regex matching in Perl, where a regex between slashes tests if a string contains a match. For example, the code checks if the string "thirteen" exists in a user's location, and prints a message if so. Regexes can perform complex tests and are powerful enough to warrant an entire book on their usage and capabilities.

Uploaded by

pankajsharma2k3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Beginner's Introduction To Perl - Part 3: Simple Matching

Perl regular expressions (regexes) allow powerful string manipulation. This document introduces simple regex matching in Perl, where a regex between slashes tests if a string contains a match. For example, the code checks if the string "thirteen" exists in a user's location, and prints a message if so. Regexes can perform complex tests and are powerful enough to warrant an entire book on their usage and capabilities.

Uploaded by

pankajsharma2k3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Beginner's Introduction to Perl - Part 3

We've covered flow control, math and string operations, and files in the first two articles in this series. Now we'll look at Perl's most powerful and interesting way of playing with strings, regular expressions, or regexes for short. (The rule is this: after the 50th time you type ``regular expression,'' you find you type ``regexp'' the next 50 times.) Regular expressions are complex enough that you could write a whole book on them (and, in fact, someone did - Mastering Regular Expressions by Jeffrey Friedl).
Simple matching

The simplest regular expressions are matching expressions. They perform tests using keywords like if, while and unless. Or, if you want to be really clever, tests that you can use with and and or. A matching regexp will return a true value if whatever you try to match occurs inside a string. When you want to use a regular expression to match against a string, you use the special =~ operator:
$user_location = "I see thirteen black cats under a ladder."; if ($user_location =~ /thirteen/) { print "Eek, bad luck!\n"; }

Notice the syntax of a regular expression: a string within a pair of slashes. The code $user_location =~ /thirteen/ asks whether the literal string thirteen occurs anywhere inside $user_location. If it does, then the test evaluates true; otherwise, it evaluates false.

You might also like