Class

class MatchData

public class MatchData {}

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

func groupNumber()

public func groupNumber(): Int64

Description: Obtains the number of capture groups.

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 match result

func matchPosition(Int64)

public func matchPosition(group: Int64): Position

Description: Obtains, based on the given index, the position of the substring matched last time by the capture group in the input string.

Parameters:

  • group: Int64: specified group

Returns:

  • Position: position of the capture group

Throws:

  • IndexOutOfBoundsException: If group is less than 0 or greater than groupNumber, this exception is thrown.
  • RegexException: If group is not 0 and there is no capture group, this exception is thrown.

func matchStr()

public func matchStr(): String

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

Returns:

Throws:

func matchStr(Int64)

public func matchStr(group: Int64): String

Description: Obtains, based on the given index, the substring matched last time by the capture group.

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

Parameters:

  • group: Int64: specified group

Returns:

Throws:

  • IndexOutOfBoundsException: If group is less than 0 or greater than groupNumber, this exception is thrown.
  • RegexException: If group is not 0 and there is no capture group, this exception is thrown.

class Matcher

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

Description: Specifies a regular expression matcher, used to scan an input sequence for matching.

Notes:

  • The maximum length of the string to be matched cannot exceed 231-1.
  • The maximum length of the string to be replaced using replaceAll cannot exceed 230-2.

init(Regex, String)

public init(re: Regex, input: String)

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

Parameters:

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

Throws:

  • RegexException: This exception is thrown upon initialization failure.
  • IllegalArgumentException: If input contains a null character or its length is greater than 231-1, this exception is thrown.

func allCount()

public func allCount(): Int64

Description: Obtains the total number of match results of a regular expression.

By default, it is the result of a thorough match. After setRegion is used, only the specified range is searched.

Returns:

  • Int64: total number of match results

func find()

public func find(): Option<MatchData>

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

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

Returns:

Throws:

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

func find(Int64)

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

Description: Resets the index position of the matcher, matches the input sequence from the position corresponding to the index, and returns the matched subsequence.

Returns:

Throws:

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

func findAll()

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

Description: Matches the entire input sequence and finds all matched subsequences.

Returns:

Throws:

  • RegexException: If memory allocation for resetting the matcher fails or applying for memory fails, this exception is thrown.

func fullMatch()

public func fullMatch(): Option<MatchData>

Description: Matches the entire input sequence.

Returns:

func getString()

public func getString(): String

Description: Obtains a matched sequence.

Returns:

func matchStart()

public func matchStart(): Option<MatchData>

Description: Matches the header of the input sequence.

Returns:

Throws:

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

func region()

public func region(): Position

Description: Returns region settings of the matcher.

Returns:

  • Position: region settings of the matcher

func replace(String)

public func replace(replacement: String): String

Description: From the current string offset position, replaces the first matched subsequence with the target string, and sets the current index position to the next position of the matched subsequence.

Parameters:

  • replacement: String: string to be replaced

Returns:

Throws:

  • RegexException: If resetting the matcher fails, this exception is thrown.
  • IllegalArgumentException: If the length of replacement is greater than 230-2 or replacement contains a null character, this exception is thrown.

func replace(String, Int64)

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

Description: Performs regular expression matching from the index position of the input sequence and replaces the first matched subsequence with the target string.

Parameters:

  • replacement: String: string to be replaced
  • index: Int64: start position of the match

Returns:

Throws:

  • IndexOutOfBoundsException: If index is less than 0, or greater than or equal to the size of the input sequence, this exception is thrown.
  • RegexException: If resetting the matcher fails, this exception is thrown.
  • IllegalArgumentException: If the length of replacement is greater than 230-2 or replacement contains a null character, this exception is thrown.

func replaceAll(String)

public func replaceAll(replacement: String): String

Description: Replaces all subsequences in the input sequence, which match the regular expression, with the target string.

Parameters:

  • replacement: String: string to be replaced

Returns:

Throws:

  • RegexException: This exception is thrown if resetting the matcher fails, applying for memory at the C side fails, or GC occurs during string replacement.
  • IllegalArgumentException: If the length of replacement is greater than 230-2 or replacement contains a null character, this exception is thrown.

func replaceAll(String, Int64)

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

Description: Replaces the first limit subsequences in the input sequence, which match the regular expression, with the specified string.

Parameters:

  • 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 sequence is replaced as many times as possible.
  • replacement: String: string to be replaced

Returns:

Throws:

  • RegexException: This exception is thrown if resetting the matcher fails, applying for memory at the C side fails, or GC occurs during string replacement.
  • IllegalArgumentException: If the length of replacement is greater than 230-2 or replacement contains a null character, this exception is thrown.

func resetRegion()

public func resetRegion(): Matcher

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

Returns:

Throws:

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

func resetString(String)

public func resetString(input: String): Matcher

Description: Resets the matching sequence and the matcher.

Parameters:

  • input: String: new matching sequence

Returns:

Throws:

  • RegexException: If resetting the matcher fails, this exception is thrown.
  • IllegalArgumentException: If the length of input is greater than 231-1 or input contains a null character, this exception is thrown.

func setRegion(Int64, Int64)

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

Description: Sets the position of a searchable region of the matcher. The specific position is determined by begin and end.

Parameters:

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

Returns:

Throws:

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

func split()

public func split(): Array<String>

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

Returns:

func split(Int64)

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

Description: Splits a given input sequence into subsequences as many as possible (limit subsequences at most) based on the regular expression.

Parameters:

  • limit: Int64: maximum number of substrings after splitting

Returns:

  • Array<String>: If limit is greater than 0, a maximum of limit substrings are returned. If limit is less than or equal to 0, the maximum number of substrings that can be obtained after splitting is returned.

Throws:

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

class Regex

public class Regex {
    public init(s: String)
    public init(s: String, option: RegexOption)
}

Description: Specifies the compilation type and input sequence.

For details about regular expression matching rules, see the regex rule set.

init(String)

public init(s: String)

Description: Creates a Regex instance, with the matching pattern being normal pattern.

Parameters:

  • s: String: regular expression

Throws:

init(String, RegexOption)

public init(s: String, option: RegexOption)

Description: Creates a Regex instance using a specified pattern.

Parameters:

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

Throws:

func matcher(String)

public func matcher(input: String): Matcher

Description: Creates a matcher.

Parameters:

  • input: String: string to be matched, with the maximum length of 231-1

Returns:

Throws:

func matches(String)

public func matches(input: String): Option<MatchData>

Description: Creates a matcher and perform exact match between input and the regular expression.

Parameters:

  • input: String: string to be matched, with the maximum length of 231-1

Returns:

Throws:

func string()

public func string(): String

Description: Obtains the input sequence of a regular expression.

Returns:

class RegexOption

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

Description: Specifies the regular expression matching pattern.

The default regular expression matching algorithm is NFA, and the maximum number of matches is 1000000.

Parent Type:

init()

public init()

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

func ignoreCase()

public func ignoreCase(): RegexOption

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

Returns:

func multiLine()

public func multiLine(): RegexOption

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

Returns:

func toString()

public func toString(): String

Description: Obtains the regular expression matching pattern represented by RegexOption.

Returns:

  • String: regular expression matching pattern