ArticleslgStudy

science

Raku rules

Raku rules is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Raku rules rather than just read about it. In short: Raku rules are the regular expression, string matching and general-purpose parsing facility of the Raku programming language, and are a core part of the language. Since Perl's pattern-matching constructs have exceeded the capabilities of formal regular expressions for some time, Raku documentation refers to them exclusively as regexes, distancing the term from the formal definition.

Key takeaways

  • Raku rules belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Raku rules to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Raku rules from memory before moving on to harder problems.

Reference excerpt

Raku rules are the regular expression, string matching and general-purpose parsing facility of the Raku programming language, and are a core part of the language. Since Perl's pattern-matching constructs have exceeded the capabilities of formal regular expressions for some time, Raku documentation refers to them exclusively as regexes, distancing the term from the formal definition. Raku provides a superset of Perl 5 features with respect to regexes, folding them into a larger framework called rules, which provide the capabilities of a parsing expression grammar, as well as acting as a closure with respect to their lexical scope. Rules are introduced with the rule keyword, which has a usage quite similar to subroutine definitions. Anonymous rules can be introduced with the regex (or rx) keyword, or simply be used inline as regexes were in Perl 5 via the m (matching) or s (substitution) operators.

History In Apocalypse 5, a document outlining the preliminary design decisions for Raku pattern matching, Larry Wall enumerated 20 problems with the "current regex culture". Among these were that Perl's regexes were "too compact and 'cute'", had "too much reliance on too few metacharacters", "little support for named captures", "little support for grammars", and "poor integration with 'real' language". Between late 2004 and mid-2005, a compiler for Raku style rules was developed for the Parrot virtual machine called Parrot Grammar Engine (PGE), which was later renamed to the more generic Parser Grammar Engine. PGE is a combination of runtime and compiler for Raku style grammars that allows any parrot-based compiler to use these tools for parsing, and also to provide rules to their runtimes. Among other Raku features, support for named captures was added to Perl 5.10 in 2007. In May 2012, the reference implementation of Raku, Rakudo, shipped its Rakudo Star monthly snapshot with a working JSON parser built entirely in Raku rules.

Changes from Perl 5 There are only six unchanged features from Perl 5's regexes:

Literals: word characters (letters, numbers and underscore) matched literally Capturing: (...) Alternatives: | Backslash escape: \ Repetition quantifiers: *, +, and ?, but not {m,n} Minimal matching suffix: *?, +?, ?? A few of the most powerful additions include:

The ability to reference rules using <rulename> to build up entire grammars. A handful of commit operators that allow the programmer to control backtracking during matching. The following changes greatly improve the readability of regexes:

Simplified non-capturing groups: [...], which are the same as Perl 5's: (?:...) Simplified code assertions: <?{...}> Allows for whitespace to be included without being matched, allowing for multiline regexes. Use \ or ' ' to express whitespace. Extended regex formatting (Perl 5's /x) is now the default.

Implicit changes Some of the features of Perl 5 regular expressions are more powerful in Raku because of their ability to encapsulate the expanded features of Raku rules. For example, in Perl 5, there were positive and negative lookahead operators (?=...) and (?!...). In Raku these same features exist, but are called <before ...> and <!before ...>. However, because before can encapsulate arbitrary rules, it can be used to express lookahead as a syntactic predicate for a grammar. For example, the following parsing expression grammar describes the classic non-context-free language { a n b n c n : n ≥ 1 } {\displaystyle \{a^{n}b^{n}c^{n}:n\geq 1\}} :

In Raku rules that would be:

Of course, given the ability to mix rules and regular code, that can be simplified even further:

However, this makes use of assertions, which is a subtly different concept in Raku rules, but more substantially different in parsing theory, making this a semantic rather than syntactic predicate. The most important difference in practice is performance. There is no way for the rule engine to know what conditions the assertion may match, so no optimization of this process can be made.

Integration with Perl In many languages, regular expressions are entered as strings, which are then passed to library routines that parse and compile them into an internal state. In Perl 5, regular expressions shared some of the lexical analysis with Perl's scanner. This simplified many aspects of regular expression usage, though it added a great deal of complexity to the scanner. In Raku, rules are part of the grammar of the language. No separate parser exists for rules, as it did in Perl 5. This means that code, embedded in rules, is parsed at the same time as the rule itself and its surrounding code. For example, it is possible to nest rules and code without re-invoking the parser:

The above is a single block of Raku code that contains an outer rule definition, an inner block of assertion code, and inside of that a regex that contains one more level of assertion.

Implementation

Keywords There are several keywords used in conjunction with Raku rules:

regex A named or anonymous regex that ignores whitespace within the regex by default. token A named or anonymous regex that implies the :ratchet modifier. rule A named or anonymous regex that implies the :ratchet and :sigspace modifiers. rx An anonymous regex that takes arbitrary delimiters such as // where regex only takes braces. m An operator form of anonymous regex that performs matches with arbitrary delimiters. mm Shorthand for m with the :sigspace modifier. s An operator form of anonymous regex that performs substitution with arbitrary delimiters. ss Shorthand for s with the :sigspace modifier. /.../ Simply placing a regex between slashes is shorthand for rx/.../. Here is an example of typical use:

Modifiers Modifiers may be placed after any of the regex keywords, and before the delimiter. If a regex is named, the modifier comes after the name. Modifiers control the way regexes are parsed and how they behave. They are always introduced with a leading : character. Some of the more important modifiers include:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Raku rules

Start with the simplest possible case. Write down what Raku rules claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Raku rules before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Raku rules ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Raku rules

In research
Raku rules appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Raku rules in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Raku rules is common in secondary-school and first-year university syllabi. It links to neighbouring topics Raku (programming language), Regular expressions, so understanding it makes those chapters shorter.
In everyday life
Look for Raku rules outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Raku rules” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Raku rules in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Raku rules means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Raku rules out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Raku rules in simple terms?

Raku rules are the regular expression, string matching and general-purpose parsing facility of the Raku programming language, and are a core part of the language. Since Perl's pattern-matching constructs have exceeded the capabilities of formal regular expressions for some time, Raku documentation…

Why does Raku rules matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Raku rules?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Raku rules.

Tags

  • Raku (programming language)
  • Regular expressions

Keep exploring