Matching Multiple Instances of Characters

Ready for more? The second group of regular expression syntax to explore is quantifiers. The patterns you've seen up to now refer to individual things or groups of individual things, but quantifiers enable you to indicate multiple instances of things—or potentially no things. These regular expression metacharacters are called quantifiers because they indicate some quantity of characters or groups of characters in the pattern you're looking for.

Perl's regular expressions include three quantifier metacharacters: ?, *, and +. Each refers to some multiple of the character or group that appears just before it in the pattern.

Optional Characters with ?

Let's start with ?, which matches a sequence that may ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.