ela
Contents

ManualPackages

Patterns

In the terminalela --spec patterns

Regular expressions, under the re prefix, in PCRE2's Perl syntax: classes ([0-9], \d, \s, \w), repeats (*, +, ?, {1,3}, their non-greedy forms with ? after them and their possessive ones with +), groups ( ), (?: ) and named ones (?<name> ), alternation |, anchors ^ $ and word boundaries \b, look-ahead (?= ) (?! ) and look-behind (?<= ) (?<! ), back-references \1 and \k<name>, Unicode properties \p{L} and \p{Greek}, and flags (?i), (?m), (?s) and (?x). A pattern is written in backticks, so its backslashes stay as they are.

Built-inDoes
re.match(pattern, s)Whether the pattern matches somewhere in s; ^ and $ pin it to the whole
re.find(pattern, s)The first match as a list, the whole match then each group, and the position it starts at, counting from 0 as $s[i] does, as two values; no match is an error, so test with re.match first or catch it with try
re.find_all(pattern, s)Every match, each such a list
re.replace(pattern, s, replacement)s with every match replaced; in replacement, $1 to $9 or ${12} is a group by number, ${name} one by name, $0 the whole match and $$ a $
if re.match(`^v\d+\.\d+\.\d+$`, $tag)    m, _ = re.find(`^v(\d+)\.(\d+)`, $tag)    major = int($m[1])end ifm, at, err = try re.find(`\s#`, $line)     # a comment after the codeif $err == null    code = $line[:$at]end ifto = re.replace(`(?<user>\w+)@(?<host>[\w.]+)`, $mail, '${user} at ${host}')
  • re.find(...)[1] reads no group: the call gives two values, and stops the script where nothing matches. ela --lint reports it and names the try that catches it, m, at, err = try re.find(...), then $m[1] where $err == null.

  • Text is UTF-8 and so is a pattern: . is one character, and \w, \d and \b take in letters and digits of every script. Bytes that are not UTF-8, as a command may print, never match but do not stop the match.

  • $ is the end of the text, not before a newline at its end; (?m) makes ^ and $ the start and end of each line.

  • A group that took no part is "". Every match find_all gives starts where the last one ended; an empty match moves on by one character, and none is given straight after a match.

  • A match may take ten million steps, keep a million places to go back to and use at most 256 MiB. A pattern that backtracks without end, as (a+)+$ on a long line of a⁠s that ends otherwise, stops at the limit with an error try catches, so no pattern hangs a script.

  • A pattern that does not parse is an error that says where, in PCRE2's words; so is a replacement that names a group the pattern lacks.

  • For a file name, str.match takes a glob (Strings helpers).