Parsing grammars: grammar-name.parse($string)Īlthough parsing a Grammar involves more than just matching a string against a regex, this powerful regex-based text destructuring tool can't be left out from this overview of common pattern matching methods. Invoking it on a string, with a Regex as an argument, matches the string against the Regex. The match method is analogous to the m/ / operator discussed above. Match method: "string".match: /pattern/, or "string".match: // Here's an example that illustrates the primary difference between the m/ / and / / syntax: However, while the rx/ / syntax may only be used with regex adverbs that affect the compilation of the regex, the m/ / syntax may additionally be used with matching adverbs that determine how the regex engine is to perform pattern matching. The match operator m/ / immediately matches the topic variable $_ against the regex following the m.Īs with the rx/ / syntax for regex definitions, the match operator may be used with adverbs in between m and the opening regex delimiter, and with delimiters other than the slash. The difference arises because the method call from within the anonymous regex / / installs a so-called 'named capture' in the Match object, while the smartmatch against the named Regex as such does not.
The different outputs of the last two statements show that these two ways of smartmatching against a named regex are not identical. This is equivalent to inserting the adverb at the beginning of the regex, but may be clearer: Secondly, the rx form allows you to insert regex adverbs between rx and the opening delimiter to modify the definition of the entire regex. The hash character # is not available as a delimiter since it is parsed as the start of a comment that runs until the end of the line. Use of a colon as a delimiter would clash with the use of adverbs, which take the form :adverb accordingly, such use of the colon is forbidden. The form rx ( abc ), however, does define a Regex object. For example, in rx() the call operator () invokes the subroutine rx. This is because identifiers that are immediately followed by parentheses are always parsed as a subroutine call. Parentheses can be used as alternative regex delimiters, but only with a space between rx and the opening delimiter. Whitespace in regex definition syntax is generally optional, except where it is required to distinguish from function call syntax (discussed hereafter). You cannot use whitespace or alphanumeric characters as delimiters. * / # definition somewhat easier on the eyes than the secondĪlthough the choice is vast, not every character may be chosen as an alternative regex delimiter: Anonymous regex definition syntaxĪn anonymous regex may be constructed in one of the following ways: Yet, the syntax for constructing anonymous and named Regex objects differs.
REGEX WORD BOUNDARY CODE
Lexical conventionsįundamentally, Raku regexes are very much like subroutines: both are code objects, and just as you can have anonymous subs and named subs, you can have anonymous and named regexes.Ī regex, whether anonymous or named, is represented by a Regex object. This page describes this language, and explains how regexes can be used to search for text patterns in strings in a process called pattern matching.
In Raku, regexes are written in a domain-specific language, i.e. In acknowledgement of this, and in an attempt to disambiguate, a regular expression in Raku is normally referred to as a regex (from: regular expression), a term that is also in common use in other programming languages. Since their inception in the 1950's, practical implementations of regular expressions, for instance in the text search and replace functions of text editors, have outgrown their strict scientific definition. In theoretical computer science and formal language theory, regular expressions are used to describe so-called regular languages. $/ changes each time a regular expression is matchedĪ regular expression is a sequence of characters that defines a certain text pattern, typically one that one wishes to find in some large body of text. Regex readability: whitespace and comments
Pattern matching against strings Table of Contents 1