Ad space (banner)
Lesson 10 / 20

Regular expression matching

This lesson covers pattern matching with regular expressions, the thing Perl is best known for.

Writing $string =~ /pattern/ tests whether a string matches a particular pattern. Perl's regular expressions are so capable that they became the model for the feature in many other languages.

The sample code does a rough email-format check with the regular expression $email =~ /^\w+@\w+\.\w+$/.

A common early stumble is remembering what the symbols mean. You pick them up gradually: \w is a letter or digit, + is one or more repetitions, ^ and $ are the start and end of the string.

In professional work, Perl's regular expressions are used heavily for pulling particular patterns out of log files and for validating input.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)