Sign in

    pcre2

    PCRE2 bindings for MoonBit

    Download zip
    Version
    0.4.5
    License
    Apache-2.0
    Last updated
    7 months ago
    Downloads
    1K

    #tonyfettes/pcre2

    This is a MoonBit binding to the PCRE2 library.

    MatchIndex

    trait MatchIndex

    impl MatchIndex for Int

    Pcre2Error

    pub suberror Pcre2Error {
    LookaroundBSK
    Compile(String, Int, UInt64)
    Code(Int)
    }

    impl Show for Pcre2Error

    Code

    type Code

    Code::jit_compile

    fn Code::jit_compile(code : Code, complete? : Bool, partial_soft? : Bool, partial_hard? : Bool) -> Unit raise Pcre2Error

    Code::matches

    fn Code::matches(self : Code, subject : StringView) -> Matches raise Pcre2Error

    Creates an iterator over all matches of the pattern in a subject string. Each match contains both numbered and named capturing groups.

    Parameters:

    • self : A compiled regular expression pattern.
    • subject : The string to search for matches.

    Returns a Matches iterator that yields each match in sequence. Each match contains information about the matched substring and any capturing groups.

    Throws Pcre2Error if an error occurs during pattern matching.

    Example:

    let pattern = @pcre2.compile("[0-9]+")
    let matches = pattern.matches("abc123def456")
    let first = matches.next().unwrap()
    inspect(first[0], content="123")
    let second = matches.next().unwrap()
    inspect(second[0], content="456")
    assert_true(matches.next() is None)

    Code::substitute

    fn Code::substitute(self : Code, subject : StringView, replacement : StringView, global? : Bool) -> StringView raise

    Match

    type Match

    Match::get

    fn[Index : MatchIndex] Match::get(self : Match, index : Index) -> StringView?

    Match::groups

    fn Match::groups(self : Match) -> Iter2[Int, StringView]

    Match::named_groups

    fn Match::named_groups(self : Match) -> Iter2[String, StringView]

    Match::op_get

    fn[Index : MatchIndex] Match::op_get(self : Match, index : Index) -> StringView

    Matches

    type Matches

    Matches::next

    fn Matches::next(self : Matches) -> Match? raise Pcre2Error

    Retrieves the next match from the iterator, advancing the internal state to find subsequent matches in the subject string.

    This method implements the core pattern matching logic, handling various edge cases including empty matches, lookaround assertions with \K, and proper Unicode character boundary handling. It maintains internal state to ensure all matches are found in sequence without duplicates or infinite loops.

    Parameters:

    • self : The Matches iterator containing the compiled pattern, subject string, and matching state.

    Returns Some(Match) containing the next match with capturing groups and named groups, or None if no more matches are found.

    Throws Pcre2Error in the following cases:

    • Pcre2Error::Code(Int) for general PCRE2 matching errors
    • Pcre2Error::Unicode(UnicodeError) for Unicode-related errors during character offset conversion
    • Pcre2Error::LookaroundBSK when encountering problematic lookaround assertions with \K

    Examples:

    let pattern = @pcre2.compile("\\d+")
    let matches = pattern.matches("abc123def456")
    let first = matches.next().unwrap()
    inspect(first[0], content="123")
    let second = matches.next().unwrap()
    inspect(second[0], content="456")
    assert_true(matches.next() is None)

    let pattern = @pcre2.compile("a*")
    let matches = pattern.matches("aabaa")
    let first = matches.next().unwrap()
    inspect(first[0], content="aa")
    let second = matches.next().unwrap()
    inspect(second[0], content="") // empty match between 'b' and 'a'

    let pattern = @pcre2.compile("(?P<num>\\d+)")
    let matches = pattern.matches("number: 42")
    let match_result = matches.next().unwrap()
    inspect(match_result["num"], content="42")
    inspect(match_result[0], content="42")

    compile

    fn compile(pattern : String) -> Code raise Pcre2Error

    config_jit

    fn config_jit() -> Bool raise

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io