Regex

public struct Regex

A Regex represents a compiled regular expression that can be applied to String objects to search for (and replace) matched patterns.

  • Attempts to create a Regex with the provided pattern. If this fails, a tuple (nil, NSError) is returned. If it succeeds, a tuple (Regex, nil) is returned.

    Declaration

    Swift

    public static func create(pattern:String) -> (Regex?, NSError?)
  • Creates a Regex with the provided String as its pattern. If the pattern is invalid, this function calls fatalError(). Hence, it is recommended that you use Regex.create() for more descriptive error messages.

    Declaration

    Swift

    public init(_ p:String)

    Parameters

    p

    A string containing a regular expression pattern.

  • Creates a Regex with the provided String as its pattern. If the pattern is invalid, this function initializes an NSError into the provided NSErrorPointer. Regex.create() is recommended, as it wraps this constructor and handles the NSErrorPointer dance for you.

    Declaration

    Swift

    public init (pattern p:String) throws

    Parameters

    p

    A string containing a regular expression pattern.

    error

    An NSErrorPointer that will contain an NSError if initialization fails.

  • Searches in string for the regular expression pattern represented by the receiver.

    Declaration

    Swift

    public func match (string:String) -> MatchResult

    Parameters

    string

    The string in which to search for matches.

  • Searches string for the regular expression pattern represented by the receiver. Any matches are replaced using the provided replacement string, which can contain substitution patterns like "$1", etc.

    Declaration

    Swift

    public func replaceMatchesIn (string:String, with replacement:String) -> (replacements:Int, string:String)

    Parameters

    string

    The string to search.

    replacement

    The replacement pattern to apply to any matches.

    Return Value

    A 2-tuple containing the number of replacements made and the transformed search string.

  • Searches string for the regular expression pattern represented by the receiver. Any matches are replaced using the provided replacement string, which can contain substitution patterns like "$1", etc.

    Declaration

    Swift

    public func replaceMatchesIn (string:String, with replacement:String) -> String

    Parameters

    string

    The string to search.

    replacement

    The replacement pattern to apply to any matches.

    Return Value

    The transformed search string.