Skip to main content

Boost.Regex

Describes how recheck checks a regular expression of the boost-regex dialect.

Engine​

recheck follows the syntax and the semantics of the perl syntax of Boost.Regex 1.92.0 (boost::regex_constants::perl, which is the default and the same as ECMAScript), searched by regex_search with match_default and the given match flags (see Flags and Units). The POSIX syntaxes (e.g. extended and basic) are not supported.

Settings​

  • type is the character type of the pattern and its input: char (default) for boost::regex, or u32regex for boost::u32regex, which uses ICU. wchar_t (boost::wregex) is not supported.
$ recheck --dialect=boost-regex '/^(a|a)*$/'
$ recheck --dialect=boost-regex --boost-regex-type=u32regex '/^(a|a)*$/'

Flags and Units​

The matching unit is byte for char (the default type), and scalar-value for u32regex (see Matching Units).

recheck supports the following syntax options and match flags of Boost.Regex, written as these flag letters.

FlagOptionInlineNotes
iicase(?i)
xmod_x(?x)
Mno_mod_m(?-m)^ and $ match at the line terminators without it.
Sno_mod_s(?-s). matches the line terminators without it.
Nnewline_altA line feed in the pattern is |.
ccollateNot supported with a range of a class.
pmatch_posixThe longest match at a start position. An error with (*COMMIT), (*PRUNE), (*SKIP) or (*THEN).
Ematch_not_nullRejects an empty match.
Imatch_not_initial_nullRejects an empty match at the start of the input.
Zmatch_not_dot_null. does not match NUL, even with (?s).
Lmatch_single_line^ and $ match only at the start and the end of the input, even with (?m).

As in Boost.Regex, ^ and $ are multi-line and . matches the line terminators by default, and $ without multi-line matches only at the end, as \z does. The line terminators are \n, \f and \r, and also U+0085, U+2028 and U+2029 in u32regex, and neither ^ nor $ matches between \r and \n. \Z matches before any line terminators at the end (e.g. a\Z matches "a\n\r").

The other syntax options (e.g. nosubs and no_empty_expressions) do not change how a valid pattern matches, so they have no flag letter. mod_s has no flag letter either, since . matches the line terminators by default. match_not_dot_newline is the same as S, and the other match flags (e.g. match_not_bol) cannot be given.

Over char, the classes are of the C locale (e.g. \w is [0-9A-Za-z_]), and i matches the ASCII letters case-insensitively. In u32regex, the classes are of ICU's general categories as Boost.Regex defines them (e.g. \w is \p{L}, \p{Nd}, \p{Mn} and _), and i is the simple case folding. \p{...} takes the class names of Boost.Regex (e.g. \p{alpha}), and in u32regex the names of the general categories too, where a class name comes first (e.g. \pL is \p{lower}).

Under i, a class matches a character whose case folding it has, and a class of the lower or the upper case letters has all the letters. For example, (?i)[@-Z] matches "[", and (?i)\p{Lt} matches no character, since an Lt character folds to an Ll one.

Syntax Limitations​

The following constructs are not supported, and they are reported as unsupported.

  • Recursion and calls of groups (e.g. (?R), (?1), (?&name) and (?P>name)).
  • Conditionals (e.g. (?(1)a|b), (?(?=a)a|b) and (?(DEFINE)...)).
  • The backtracking control verbs other than (*FAIL) and (*F) (e.g. (*PRUNE) and (*ACCEPT)).
  • The constructs depending on the collation of the locale: a range of a class with c, the collating elements (e.g. [[.space.]]) and the equivalence classes (e.g. [[=a=]]).
  • \N{...} (e.g. \N{newline}), which is a collating element.

Approximations​

  • Boost.Regex stops a matching with error_complexity when it takes too many steps, and with error_stack when its backtracking stack overflows, which recheck does not model. The limit of the steps grows with the lengths of the input and of the pattern, so most attacks of a short pattern end with the limit in less than a second, and the runtime-attack verification reports them as an engine limit. The stack overflows even on a linear matching (e.g. ^(a)*$ on "a" repeated 100,000 times).
  • recheck does not reproduce the following behaviors of Boost.Regex.
    • A back-reference inside its own group matches the empty string, since Boost.Regex sets only the start of the capture on entering the group (e.g. ^(a\1?){4}$ does not match "aaaaaaaaaa"); recheck matches the capture of the previous iteration.
    • The captures in an atomic group or a look-ahead are not restored on backtracking (e.g. (?>(a))b|\1 matches "ac"); recheck restores them.
    • In u32regex, the line terminators and \v are compared by the low 16 bits of a code point (e.g. U+12028 is a line terminator); recheck compares the code points.
    • In u32regex, [[:print:]] matches \t, \n and the other spaces of ICU's u_isspace too; recheck matches the characters out of the general category C.
    • The negated escape classes in a class are joined before the negation (e.g. [\D\W] does not match "a"); recheck joins them as the other items.
    • Under i, \X takes U+0345 as not a combining character, since its case folding U+03B9 is not one; recheck takes it as one.
    • A quantifier after (?i) or (?-i), which repeats it as an empty group, undoes the change of i for the characters after it, but not for the classes (e.g. a(?i)+b does not match "aB", but a(?i)+[b] does); recheck changes i for both.

Runtime-Attack Verification​

  • Command: c++ <file>.cpp -o <file> once, with -licui18n -licuuc -licudata after it for u32regex, and then the compiled program.
  • Matching: regex_search with match_results (u32regex_search for u32regex), match_default and the given match flags, with Boost.Regex's default limits.
  • Engine limits: error_complexity.
  • Engine errors: error_stack, std::bad_alloc.
  • Environment variable: RECHECK_BOOST_REGEX.

RECHECK_BOOST_REGEX replaces c++ with a command line of a C++ compiler, which finds the headers of Boost, and ICU's headers and libraries for u32regex. recheck adds the program, -o, the compiled program and ICU's libraries after it. Boost.Regex needs no library of its own. The headers of Boost.Regex alone (e.g. of the boostorg/regex repository) are enough with boost/version.hpp of Boost.Config and -DBOOST_REGEX_STANDALONE, which keeps Boost.Regex from using the other libraries of Boost. For example, the following uses Boost and ICU of Homebrew.

$ export RECHECK_BOOST_REGEX="c++ -std=c++17 -O2 -I$(brew --prefix)/include -I$(brew --prefix icu4c@79)/include -L$(brew --prefix icu4c@79)/lib"
$ recheck --dialect=boost-regex --runtime-attack-timeout=1s '/^(a|a)*$/'

recheck compiles the program once for each command line and version of the compiler, and the next runs reuse the compiled program. -O2 matters, since the matching without optimizations takes about ten times as long. A macro of Boost.Regex on the command line (e.g. -DBOOST_REGEX_MAX_STATE_COUNT=1000000000) changes the limits as in your program. The runtime is boost-regex with the version of Boost (e.g. boost-regex 1.92.0). A failed compilation makes the runtime-attack verification fail with an unexpected error telling the output of the compiler.