Perl 6 Programming/Regular Expressions

Regular ExpressionsEdit

Regular expressions are a tool for specifying and searching for patterns in strings, among other things. Regular expressions were a popular and powerful part of Perl 5, although they gradually grew and expanded in successive versions of that language in a way that was difficult to follow and implement.

Perl 5's regular expressions became increasingly difficult to use and understand as more operators and metacharacters were added to the engine. It was decided that Perl 6 would break from this syntax and rewrite regular expressions from the ground-up to be more flexible and more integrated into the language. In Perl 6, they are known simply as regexes, and have become significantly more powerful.

Perl 6 supports regexes in two ways: It has a legacy mode that supports Perl 5-style regular expressions, and it has a normal mode that supports the new style of regexes.

Basic QuantifiersEdit

Regexes describe patterns in string data that can be searched for and acted upon. One of the most basic patterns to search for is a repetition pattern. To describe repetition, there are a number of quantifiers that can be used:

Op What It Means Example Explanation
* "zero or more of" B A* Accepts a string with a 'B' followed by any number of 'A' characters, even zero of them. B, BAAAAA, etc
+ "one or more of" B A+ Accepts a string with a 'B', followed by at least one 'A'. Example: BAAA, or BA but not B
? "one or zero" B A? Matches a 'B', optionally followed by one 'A'. B or BA
** "this many" B A**5 Matches a 'B' followed by exactly 5 'A' characters. BAAAAA
B A ** 2..5 Matches a 'B' followed by at least two 'A' and no more then 5 'A'. BAA, BAAA, BAAAA, BAAAAA
Last modified on 4 February 2009, at 16:25