Skip to content

parse_pattern

Reads a setting that is either a regular expression or a glob: /source/flags is a regular expression when every flag is valid (d, g, i, m, s, u, v, y), anything else is a glob. For font substitution rules, ignore lists, filters. Note that /usr/lib/img is the regular expression usr/lib with the flags img: for paths, build a Pattern::Glob yourself.

The regular expression is returned as text, since this crate has no regex engine; the source is not validated. This differs from the TypeScript parsePatternOrRegExp, which compiles the expression and throws a SyntaxError when it is invalid or repeats a flag (/a/ii): here both come back as a Pattern::RegExp, and the error appears when you compile it. The flags are the JavaScript ones; an engine such as the regex crate knows only some of them (i, m, s, u), so write rules with those to read the same on both sides.

use helpers4::string::parse_pattern;

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.11", default-features = false, features = ["string"] }
pub fn parse_pattern(pattern: &str) -> Pattern
ParameterTypeDescription
pattern&strA /source/flags regular expression or a glob.

Pattern — The Pattern.

use helpers4::string::{parse_pattern, Pattern};

assert_eq!(
    parse_pattern("/^SF Pro/i"),
    Pattern::RegExp { source: "^SF Pro".to_string(), flags: "i".to_string() }
);
assert_eq!(parse_pattern("/src/*.ts/x"), Pattern::Glob("/src/*.ts/x".to_string()));

src/string/parse_pattern.rs