Classes

class Matcher (deprecated)

public class Matcher {
    public init(re: Regex, input: String)
}

Description: Specifies a regular expression matcher, which is used to search the input sequence for a match.

NOTE

This class will be deprecated in future releases and Regex will be used instead.

init(Regex, String)

public init(re: Regex, input: String)

Description: Creates a matcher instance using the passed regular expression and input sequence.

Parameters:

  • re: Regex: regular expression
  • input: String: input sequence

func allCount()

public func allCount(): Int64

Description: Obtains the total number of matches for a regular expression.

By default, the matching is performed from the beginning to the end. However, if setRegion is used, only the specified region will be searched.

Returns:

  • Int64: total number of matches

func find()

public func find(): Option<MatchData>

Description: Searches from the current string offset position for the first matched subsequence.

When the find function is called for the first time, the current offset position is the position of the first character following the last matched subsequence. When the find function is called again, the matching starts from the current position.

Returns:

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

func find(Int64)

public func find(index: Int64): Option<MatchData>

Description: Resets the index position of the matcher to search the input sequence from the position indicated by index and return the matched subsequences.

Returns:

Throws:

  • IndexOutOfBoundsException: If the value of index is less than 0 or greater than or equal to the size of the input sequence, this exception is thrown.
  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

func findAll()

public func findAll(): Option<Array<MatchData>>

Description: Searches the input sequence for all matched subsequences.

Returns:

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

func fullMatch()

public func fullMatch(): Option<MatchData>

Description: Matches the entire input sequence.

Returns:

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

func getString()

public func getString(): String

Description: Obtains the sequence to be matched.

Returns:

  • String: sequence to be matched

func matchStart()

public func matchStart(): Option<MatchData>

Description: Matches the header of the input sequence.

Returns:

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

func region()

public func region(): Position

Description: Returns the region settings of the matcher.

Returns:

  • Position: region settings of the matcher

func replace(String)

public func replace(replacement: String): String

Description: Replaces the first matched subsequence (searched from the current string offset position) with the target string and sets the current index position to the position following the matched subsequence.

Parameters:

  • replacement: String: specified replacement string

Returns:

  • String: string after replacement

func replace(String, Int64)

public func replace(replacement: String, index: Int64): String

Description: Replaces the first matched subsequence (searched from the index position of the input sequence) with the target string.

Parameters:

  • replacement: String: specified replacement string
  • index: Int64: start position where matching starts

Returns:

  • String: string after replacement

Throws:

  • IndexOutOfBoundsException: If the value of index is less than 0 or greater than or equal to the size of the input sequence, this exception is thrown.

func replaceAll(String)

public func replaceAll(replacement: String): String

Description: Replaces all the subsequences (in the input sequence) that match the regular expression with the target string.

Parameters:

  • replacement: String: specified replacement string

Returns:

  • String: string after replacement

func replaceAll(String, Int64)

public func replaceAll(replacement: String, limit: Int64): String

Description: Replaces the first limit subsequences (in the input sequence) that match the regular expression with a specified replacement string.

Parameters:

  • replacement: String: specified replacement string
  • limit: Int64: number of replacements. If the value of limit is 0, the original sequence is returned. If the value of limit is a negative number, the replacement occurs as many times as possible.

Returns:

  • String: string after replacement

func resetRegion()

public func resetRegion(): Matcher

Description: Resets the start and end positions of the matcher.

Returns:

func resetString(String)

public func resetString(input: String): Matcher

Description: Resets the sequence to be matched and the matcher.

Parameters:

  • input: String: new sequence to be matched

Returns:

func setRegion(Int64, Int64)

public func setRegion(beginIndex: Int64, endIndex: Int64): Matcher

Description: Sets the position information of a region searchable to the matcher. The specific positions are determined by beginIndex and endIndex.

Parameters:

  • beginIndex: Int64: start position of the region
  • endIndex: Int64: end position of the region

Returns:

Throws:

  • IndexOutOfBoundsException: This exception is thrown when the value of beginIndex is less than 0 or greater than the size of the input sequence, the value of endIndex is less than 0 or greater than the size of the input sequence, or the value of beginIndex is greater than the value of endIndex.

func split()

public func split(): Array<String>

Description: Splits the input sequence into as many subsequences as possible based on the regular expression.

Returns:

func split(Int64)

public func split(limit: Int64): Array<String>

Description: Splits the input sequence into as many subsequences as possible based on the regular expression (returning a maximum of limit substrings).

Parameters:

  • limit: Int64: maximum number of substrings to be returned

Returns:

  • Array<String>: If the value of limit is greater than 0, a maximum of limit substrings are returned. If the value of limit is less than or equal to 0, all the resulting substrings are returned.

class Regex

public class Regex {
    public init(pattern: String, flags: Array<RegexFlag>)
    public init(pattern: String, option: RegexOption)
}

Description: Specifies the compilation type and creates a regular expression instance.

For details about the regular expression matching rules, see regex Rule Set.

init(String, Array<RegexFlag>)

public init(pattern: String, flags: Array<RegexFlag>)

Description: Creates a Regex instance.

Parameters:

  • pattern: String: regular expression
  • flags: Array<RegexFlag>: pattern list for regular expression matching

Throws:

  • RegexException: If the initialization fails, this exception is thrown.

init(String, RegexOption) (deprecated)

public init(pattern: String, option: RegexOption)

Description: Creates a Regex instance using a specified pattern.

NOTE

This function will be deprecated in future releases and init(String, Array<RegexFlag>) will be used instead.

Parameters:

  • pattern: String: regular expression
  • option: RegexOption: regular expression matching pattern

Throws:

  • RegexException: If the initialization fails, this exception is thrown.

func find(String, Bool)

public func find(input: String, group!: Bool = false): Option<MatchData>

Description: Searches for the first matched subsequence.

Parameters:

  • input: String: sequence to be matched
  • group!: Bool: whether to enable capture group extraction

Returns:

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

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 findAll(String, Bool)

public func findAll(input: String, group!: Bool = false): Array<MatchData>

Description: Searches the input sequence for all matched subsequences.

Parameters:

  • input: String: sequence to be matched
  • group!: Bool: whether to enable capture group extraction

Returns:

  • Array<MatchData>: array for storing the matching result. If no match is found, the array is empty.

Throws:

  • RegexException: If a match is found but the matched information fails to be extracted, this exception is thrown.

Examples:

import std.regex.*

main(): Unit {
    let r = Regex("ab")
    let arr = r.findAll("ababaaab")
    let iter = arr.iterator()
    println(arr.size)
    while (true) {
        match (iter.next()) {
            case Some(i) => println(i.matchString())
            case None => break
        }
    }
}

Results:

3
ab
ab
ab

func getNamedGroups()

public func getNamedGroups(): Map<String, Int64>

Description: Obtains the name and index mapping of a named capture group.

Returns:

  • Map<String, Int64>: name and index mapping of the named capture group

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    let arr = r.findAll("2024-10-24&2025-01-01", group: true)
    for (md in arr) {
        println("# found: `${md.matchString()}` and groupCount: ${md.groupCount()}")
        for ((name, index) in r.getNamedGroups()) {
            println("${name} => ${index}")
        }
    }
}

Results:

# found: `2024-10-24` and groupCount: 3
day => 3
month => 2
year => 1
# found: `2025-01-01` and groupCount: 3
day => 3
month => 2
year => 1

func lazyFindAll(String, Bool)

public func lazyFindAll(input: String, group!: Bool = false): Iterator<MatchData>

Description: Matches the entire input sequence and obtains a matching iterator.

Parameters:

  • input: String: sequence to be matched
  • group!: Bool: whether to enable capture group extraction

Returns:

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()}")
                for ((name, index) in r.getNamedGroups()) {
                    println("${name} => ${index}")
                }
            case None => break
        }
    }
}

Results:

# found: `2024-10-24` and groupCount: 3
day => 3
month => 2
year => 1
# found: `2025-01-01` and groupCount: 3
day => 3
month => 2
year => 1

func matcher(String) (deprecated)

public func matcher(input: String): Matcher

Description: Creates a matcher.

NOTE

This function will be deprecated in future releases.

Parameters:

  • input: String: string to be matched

Returns:

func matches(String)

public func matches(input: String): Bool

Description: Checks whether the input parameter input matches the regular expression.

Parameters:

  • input: String: string to be matched

Returns:

  • Bool: If a match is found, true is returned. If no match is found, false is returned.

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    println(r.matches("2024-10-24&2025-01-01"))
}

Results:

true

func replace(String, String)

public func replace(input: String, replacement: String): String

Description: Replaces the first matched subsequence (searched from the start position of the current string) with the target string.

Parameters:

  • input: String: sequence to be matched
  • replacement: String: specified replacement string

Returns:

  • String: string after replacement

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    println(r.replace("2024-10-24&2025-01-01", "time"))
}

Results:

time&2025-01-01

func replace(String, String, Int64)

public func replace(input: String, replacement: String, index: Int64): String

Description: Replaces the first matched subsequence (searched from the index position of the input sequence) with the target string.

Parameters:

  • input: String: sequence to be matched
  • replacement: String: specified replacement string
  • index: Int64: start position where matching starts

Returns:

  • String: string after replacement

Throws:

  • IndexOutOfBoundsException: If the value of index is less than 0 or greater than or equal to the size of the input sequence, this exception is thrown.

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    println(r.replace("2024-10-24&2025-01-01", "time", 10))
}

Results:

2024-10-24&time

func replaceAll(String, String)

public func replaceAll(input: String, replacement: String): String

Description: Replaces all the subsequences (in the input sequence) that match the regular expression with the target string.

Parameters:

  • input: String: sequence to be matched
  • replacement: String: specified replacement string

Returns:

  • String: string after replacement

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    println(r.replaceAll("2024-10-24&2025-01-01", "time"))
}

Results:

time&time

func replaceAll(String, String, Int64)

public func replaceAll(input: String, replacement: String, limit: Int64): String

Description: Replaces the first limit subsequences (in the input sequence) that match the regular expression with a specified replacement string.

Parameters:

  • input: String: sequence to be matched
  • replacement: String: specified replacement string
  • limit: Int64: number of replacements If the value of limit is 0, the original sequence is returned. If the value of limit is a negative number, the replacement occurs as many times as possible.

Returns:

  • String: string after replacement

Examples:

import std.regex.*

main(): Unit {
    let r = Regex(#"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"#)
    println(r.replaceAll("2019-4-5&2024-10-24&2025-01-01", "time", 10))
}

Results:

2019-4-5&time&time

func split(String)

public func split(input: String): Array<String>

Description: Splits the input sequence into as many subsequences as possible based on the regular expression.

Parameters:

  • input: String: sequence to be matched

Returns:

Examples:

import std.regex.*

main(): Unit {
    let r = Regex("&")
    for (subStr in r.split("2019-4-5&2024-10-24&2025-01-01")) {
        println(subStr)
    }
}

Results:

2019-4-5
2024-10-24
2025-01-01

func split(String, Int64)

public func split(input: String, limit: Int64): Array<String>

Description: Splits the input sequence into as many subsequences as possible based on the regular expression (returning a maximum of limit substrings).

Parameters:

  • input: String: sequence to be matched
  • limit: Int64: maximum number of substrings to be returned

Returns:

  • Array<String>: If the value of limit is greater than 0, a maximum of limit substrings are returned. If the value of limit is less than or equal to 0, all the resulting substrings are returned.

func string()

public func string(): String

Description: Obtains the input sequence for regular expression matching.

Returns:

class RegexOption (deprecated)

public class RegexOption <: ToString {
    public init()
}

Description: Specifies the regular expression matching pattern.

NOTE

This class will be deprecated in future releases and RegexFlag will be used instead.

Parent types:

init()

public init()

Description: Creates a RegexOption instance with the matching pattern being normal pattern (NORMAL).

func ignoreCase()

public func ignoreCase(): RegexOption

Description: Modifies RegexOption by changing the matching pattern to case-insensitive pattern (IGNORECASE).

Returns:

func multiLine()

public func multiLine(): RegexOption

Description: Modifies RegexOption by changing the matching pattern to multi-line text pattern (MULTILINE).

Returns:

func toString()

public func toString(): String

Description: Obtains the regular expression matching pattern currently specified by RegexOption.

Returns:

  • String: regular expression matching pattern