RegularExpression

public class RegularExpression

This class represents a compiled regular expression.

  • Compile a regular expression.

    Note

    Matching will be performed case insensitive.

    Declaration

    Swift

    public init?(_ pattern: String)

    Return Value

    nil if the expression cannot be compiled, i.e. if it is invalid.

  • Tests if the regular expression matches a given string input.

    Example

    let pattern = RegularExpression("^The")!
    let sentence = "The world is flat."
    if pattern.matches(sentence) {
        // The sentence starts with "The"
    }
    

    Declaration

    Swift

    public func matches(_ input: String) -> Bool

    Parameters

    input

    The string that shall be tested.

    Return Value

    true if the expression matches the string, false otherwise.