PATTERNS(7) | Miscellaneous Information Manual | PATTERNS(7) |
patterns
— Lua's
pattern matching rules
Pattern matching in httpd(8) is based on the implementation of the Lua scripting language and provides a simple and fast alternative to the regular expressions (REs) that are described in re_format(7). Patterns are described by regular strings, which are interpreted as patterns by the pattern-matching “find” and “match” functions. This document describes the syntax and the meaning (that is, what they match) of these strings.
A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:
The interaction between ranges and classes is not defined. Therefore, patterns like ‘[%a-z]’ or ‘[a-%%]’ have no meaning.
For all classes represented by single letters ( ‘%a’, ‘%c’, etc.), the corresponding uppercase letter represents the complement of the class. For instance, ‘%S’ represents all non-space characters.
The definitions of letter, space, and other character groups depend on the current locale. In particular, the class ‘[a-z]’ may not be equivalent to ‘%l’.
A pattern item can be
A pattern is a sequence of pattern items. A caret ‘^’ at the beginning of a pattern anchors the match at the beginning of the subject string. A ‘$’ at the end of a pattern anchors the match at the end of the subject string. At other positions, ‘^’ and ‘$’ have no special meaning and represent themselves.
A pattern can contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3.
As a special case, the empty capture ‘()’ captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 2 and 4.
fnmatch(3), re_format(7), httpd(8)
Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, Patterns, Lua 5.3 Reference Manual, https://www.lua.org/manual/5.3/manual.html#6.4.1, Lua.org, PUC-Rio, June 2015.
The first implementation of the pattern rules were introduced with Lua 2.5. Almost twenty years later, an implementation based on Lua 5.3.1 appeared in OpenBSD 5.8.
The pattern matching is derived from the original implementation of the Lua scripting language written by Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo at PUC-Rio. It was turned into a native C API for httpd(8) by Reyk Floeter <reyk@openbsd.org>.
A notable difference with the Lua implementation is the position in the string returned by captures. It follows the C-style indexing (position starting from 0) instead of Lua-style indexing (position starting from 1).
February 18, 2022 | OpenBSD-current |