NSRegularExpression

public extension NSRegularExpression
  • SwifterSwift: Enumerates the string allowing the Block to handle each regular expression match.

  • Undocumented

    Declaration

    Swift

    func enumerateMatches(in string: String,
                          options: MatchingOptions = [],
                          range: Range<String.Index>,
                          using block: (_ result: NSTextCheckingResult?, _ flags: MatchingFlags, _ stop: inout Bool) -> Void)
  • SwifterSwift: Returns an array containing all the matches of the regular expression in the string.

    Declaration

    Swift

    func matches(in string: String,
                 options: MatchingOptions = [],
                 range: Range<String.Index>) -> [NSTextCheckingResult]

    Parameters

    string

    The string to search.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    Return Value

    An array of NSTextCheckingResult objects. Each result gives the overall matched range via its range property, and the range of each individual capture group via its range(at:) method. The range {NSNotFound, 0} is returned if one of the capture groups did not participate in this particular match.

  • SwifterSwift: Returns the number of matches of the regular expression within the specified range of the string.

    Declaration

    Swift

    func numberOfMatches(in string: String,
                         options: MatchingOptions = [],
                         range: Range<String.Index>) -> Int

    Parameters

    string

    The string to search.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    Return Value

    The number of matches of the regular expression.

  • SwifterSwift: Returns the first match of the regular expression within the specified range of the string.

    Declaration

    Swift

    func firstMatch(in string: String,
                    options: MatchingOptions = [],
                    range: Range<String.Index>) -> NSTextCheckingResult?

    Parameters

    string

    The string to search.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    Return Value

    An NSTextCheckingResult object. This result gives the overall matched range via its range property, and the range of each individual capture group via its range(at:) method. The range {NSNotFound, 0} is returned if one of the capture groups did not participate in this particular match.

  • SwifterSwift: Returns the range of the first match of the regular expression within the specified range of the string.

    Declaration

    Swift

    func rangeOfFirstMatch(in string: String,
                           options: MatchingOptions = [],
                           range: Range<String.Index>) -> Range<String.Index>?

    Parameters

    string

    The string to search.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    Return Value

    The range of the first match. Returns nil if no match is found.

  • SwifterSwift: Returns a new string containing matching regular expressions replaced with the template string.

    Declaration

    Swift

    func stringByReplacingMatches(in string: String,
                                  options: MatchingOptions = [],
                                  range: Range<String.Index>,
                                  withTemplate templ: String) -> String

    Parameters

    string

    The string to search for values within.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    templ

    The substitution template used when replacing matching instances.

    Return Value

    A string with matching regular expressions replaced by the template string.

  • SwifterSwift: Replaces regular expression matches within the mutable string using the template string.

    Declaration

    Swift

    @discardableResult
    func replaceMatches(in string: inout String,
                        options: MatchingOptions = [],
                        range: Range<String.Index>,
                        withTemplate templ: String) -> Int

    Parameters

    string

    The mutable string to search and replace values within.

    options

    The matching options to use. See NSRegularExpression.MatchingOptions for possible values.

    range

    The range of the string to search.

    templ

    The substitution template used when replacing matching instances.

    Return Value

    The number of matches.