Regex

public struct Regex

A Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the "xyz".regex and by init(pattern:) constructor.

  • Inside pattern which is immutable after initialization.

    Declaration

    Swift

    public let pattern: String
  • A regular expression converted from a pattern

    Declaration

    Swift

    public let regexp: NSRegularExpression
  • Designated intializer for Regexp which encapsulates a NSRegularExpression instance inside. Converts the passed-in pattern to a NSRegularExpression inside.

    Declaration

    Swift

    public init(_ pattern: String, literal: Bool = false)

    Parameters

    pattern

    A string pattern

    literal

    A bool value indicates the NSRegularExpression matches a literal pattern

  • Invokes its NSRegularExpression#firstMatch(in:options:range:) method first to check it str is matched with current regexp. Returns nil if NSRegularExpression#firstMatch(in:options:range:) returns false, or this methods will encapsulates all the data inside a MatchData struct.

    "[a-e]+".regex.match("hello") { data in
        print(data.match)       #=> "e"
    }
    
    "[a-e]+".regex.match("hello") { data in
        print(data.match)       #=> "e"
    }
    

    If the second parameter is present, it specifies the position in the string to begin the search.

    "[a-e]+".regex.match("hello", 1) { data in
        print(data.match)       #=> "e"
        print(data.range)       #=> NSRange< loc: 1, length: 1 }>
    }
    

    Declaration

    Swift

    public func match(_ str: String, _ pos: Int = 0, closure: ((MatchData) -> Void)? = nil) -> MatchData?

    Parameters

    str

    A string

    pos

    The position in the string to begin the search

    closure

    A closure invoked if there is a match

    Return Value

    A MatchData instance contains all match results in it

  • Invokes its NSRegularExpression#matches(in:options:range:) method to check it str is matched with current regexp. Returns [] if returns false, or this methods will encapsulates all the data inside a MatchData struct, and returns [MatchData]

    "[aeiou]+".regex.scan("hello") { data in
        print(data.match)       #=> e, o
    }
    

    Declaration

    Swift

    public func scan(_ str: String, closure: ((MatchData) -> Void)? = nil) -> [MatchData]

    Parameters

    pattern

    A string

    closure

    A closure invoked if there is a match

    Return Value

    An array of MatchData instance contains all match results in it

  • Invokes NSRegularExpression#stringByReplacingMatches(in:options:range:withTemplate:) method to replace original match result with template, $1, $2… are used to capture the match group in the str.

    let str = "hello"
    "l".regex.replace(str, "abc")       #=> "heabcabco"
    "le".regex.replace(str, "lll")      #=> "hello"
    ".".literal.replace(str, "lll")     #=> "hello"
    ".".regex.replace(str, "lll")       #=> "lllllllllllllll"
    "^he".regex.replace(str, "lll")     #=> "lllllo"
    
    "\\b(?<!['’`])[a-z]".regex.replace("my name is draven", "a")      #=> "ay aame as araven"
    

    Declaration

    Swift

    public func replace(_ str: String,  _ template: String) -> String

    Parameters

    str

    A string waiting for replacing

    template

    A template string used to replace original str

    Return Value

    A new string with all matching result replaced by template

  • Returns true if string is match with Regex

    let regex = "hello".regex
    if regex =~ "hello world" {
        print("this will match")
    }
    
    if regex =~ "world" {
        print("this won't match")
    }
    

    Declaration

    Swift

    public static func =~(regex: Regex, str: String) -> Bool

    Parameters

    regex

    A Regex struct used to match the string

    str

    A string waiting to match

    Return Value

    A bool value indicates whether the str is matched with the Regexp

  • Returns true if string is match with Regex

    let regex = "hello".regex
    if "hello world" =~ regex {
        print("this will match")
    }
    
    if "world" =~ regex {
        print("this won't match")
    }
    

    Declaration

    Swift

    public static func =~(str: String, regex: Regex) -> Bool

    Parameters

    str

    A string waiting to match

    regex

    A Regex struct used to match the string

    Return Value

    A bool value indicates whether the str is matched with the Regexp