Structs

struct MatchData

public struct MatchData {}

Description: Stores regular expression matching results, and provides functions for querying the regular expression matching results.

func groupCount()

public func groupCount(): Int64

Description: Obtains the number of capture groups.

Returns:

  • Int64: number of capture groups

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    let iter = r.lazyFindAll("2024-10-24&2025-01-01", group: true)
    while (true) {
        match (iter.next()) {
            case Some(md) => println("# found: `${md.matchString()}` and groupCount: ${md.groupCount()}")
            case None => break
        }
    }
}

Results:

# found: `2024-10-24` and groupCount: 3
# found: `2025-01-01` and groupCount: 3

func groupNumber() (deprecated)

public func groupNumber(): Int64

Description: Obtains the number of capture groups.

NOTE

This function will be deprecated in future releases and groupCount() will be used instead.

Returns:

  • Int64: number of capture groups

func matchPosition()

public func matchPosition(): Position

Description: Obtains the indexes of the start position and end position of the substring matched last time in the input string.

Returns:

  • Position: position of the matching result

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    let iter = r.lazyFindAll("2024-10-24&2025-01-01", group: true)
    while (true) {
        match (iter.next()) {
            case Some(md) =>
                println("# found: ${md.matchString()} and groupCount: ${md.groupCount()}")
                let pos = md.matchPosition(0)
                println("pos: [${pos.start}, ${pos.end}]")
            case None => break
        }
    }
}

Results:

# found: 2024-10-24 and groupCount: 3
pos: [0, 10]
# found: 2025-01-01 and groupCount: 3
pos: [11, 21]

func matchPosition(Int64)

public func matchPosition(group: Int64): Position

Description: Obtains the position of the substring matched last time by the capture group with a specified index in the input string.

Parameters:

  • group: Int64: specified group

Returns:

  • Position: position of the substring matched by the capture group

Throws:

  • IllegalArgumentException: If capture group extraction is disabled, or the value of group is less than 0 or greater than the value of groupCount, this exception is thrown.

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    let iter = r.lazyFindAll("2024-10-24&2025-01-01", group: true)
    while (true) {
        match (iter.next()) {
            case Some(md) =>
                println("# found: ${md.matchString()} and groupCount: ${md.groupCount()}")
                /* The month index of the capture group is 2. */
                let pos = md.matchPosition(2)
                println("# month: [${pos.start}, ${pos.end}]")
            case None => break
        }
    }
}

Results:

# found: 2024-10-24 and groupCount: 3
# month: [5, 7]
# found: 2025-01-01 and groupCount: 3
# month: [16, 18]

func matchPosition(String)

public func matchPosition(group: String): Position

Description: Obtains the position of the substring matched last time by the capture group with a specified capture group name in the input string.

Parameters:

  • group: String: specified capture group name

Returns:

  • Position: position of the substring matched by the capture group

Throws:

  • IllegalArgumentException: If capture group extraction is disabled or the capture group name does not exist, this exception is thrown.

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    let iter = r.lazyFindAll("2024-10-24&2025-01-01", group: true)
    while (true) {
        match (iter.next()) {
            case Some(md) =>
                println("# found: ${md.matchString()} and groupCount: ${md.groupCount()}")
                let pos = md.matchPosition("year")
                println("year: [${pos.start}, ${pos.end}]")
            case None => break
        }
    }
}

Results:

# found: 2024-10-24 and groupCount: 3
year: [0, 4]
# found: 2025-01-01 and groupCount: 3
year: [11, 15]

func matchString()

public func matchString(): String

Description: Obtains the substring matched last time. The result is the same as that of calling matchString(0).

Returns:

Examples:

import std.regex.*

main(): Unit {
    let r1 = Regex("ab")
    let r2 = Regex("ab", IgnoreCase)
    match (r1.find("aB")) {
        case Some(r) => println(r.matchString())
        case None => println("None")
    }
    match (r2.find("aB")) {
        case Some(r) => println(r.matchString())
        case None => println("None")
    }
}

Results:

None
aB

func matchString(Int64)

public func matchString(group: Int64): String

Description: Obtains the substring matched last time by the capture group with a specified index.

The capture group index starts from 1. If the index is 0, the matching result of the entire regular expression is obtained.

Parameters:

  • group: Int64: specified group

Returns:

Throws:

  • IllegalArgumentException: If capture group extraction is disabled, or the value of group is less than 0 or greater than the value of groupCount, this exception is thrown.

func matchString(String)

public func matchString(group: String): String

Description: Obtains the substring matched last time by the capture group with a specified capture group name.

Parameters:

  • group: String: specified capture group name

Returns:

Throws:

  • IllegalArgumentException: If capture group extraction is disabled or the capture group name does not exist, this exception is thrown.

struct Position

public struct Position {
    public let end: Int64
    public let start: Int64
}

Description: Stores position information, indicating a left-closed, right-open interval.

let end

public let end: Int64

Description: Specifies the end position of the interval.

Type: Int64

let start

public let start: Int64

Description: Specifies the start position of the interval.

Type: Int64