Raku Programming/Regular Expressions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Regular Expressions[edit | edit source]

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, although they gradually grew and expanded in successive versions of that language in a way that was difficult to follow and implement.

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

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

Basic Quantifiers[edit | edit source]

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 than 5 'A'. BAA, BAAA, BAAAA, BAAAAA