Function lua_pattern::try_to_regex

source ·
pub fn try_to_regex(
    pattern: &[PatternObject],
    allow_capture_refs: bool,
    allow_lookaround: bool
) -> Result<String, ToRegexError>
Available on crate feature to-regex only.
Expand description

Try to convert a parsed Lua pattern into a regular expression string.

The allow_capture_refs parameter specifies whether to allow backreferences to capture groups. Set this to false when using the output with the regex crate, or to true when using the fancy-regex crate.

Returns

The function returns a String if the conversion was successful, and a ToRegexError otherwise.

Errors

Converting a Lua pattern to a RegEx can fail in up to three ways.

  1. Lua patterns support balanced bracket matching using the %b operator. This is not supported by RegEx. Thus, an error will be returned if the input pattern makes use of this feature.
  2. Lua patterns support references to previous capture groups. Some RegEx engines also support this feature, but not all. For this reason, uses of such backreferences will result in an error, if allow_capture_refs is set to false.
  3. Lua patterns support so-called frontier patterns. Their behaviour can be emulated using lookaround, but only some RegEx engines support that. Therefore, if the input includes a frontier pattern and allow_lookaround is set to false, an error will be returned.

Also see ToRegexError for further information.