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
Regexpwhich encapsulates aNSRegularExpressioninstance inside. Converts the passed-in pattern to aNSRegularExpressioninside.Declaration
Swift
public init(_ pattern: String, literal: Bool = false)Parameters
patternA string pattern
literalA bool value indicates the
NSRegularExpressionmatches a literal pattern -
Invokes its
NSRegularExpression#firstMatch(in:options:range:)method first to check it str is matched with currentregexp. ReturnsnilifNSRegularExpression#firstMatch(in:options:range:)returns false, or this methods will encapsulates all the data inside aMatchDatastruct."[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
Parameters
strA string
posThe position in the string to begin the search
closureA closure invoked if there is a match
Return Value
A
MatchDatainstance contains all match results in it -
Invokes its
NSRegularExpression#matches(in:options:range:)method to check it str is matched with currentregexp. Returns[]if returns false, or this methods will encapsulates all the data inside aMatchDatastruct, and returns[MatchData]"[aeiou]+".regex.scan("hello") { data in print(data.match) #=> e, o }Declaration
Parameters
patternA string
closureA closure invoked if there is a match
Return Value
An array of
MatchDatainstance 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) -> StringParameters
strA string waiting for replacing
templateA 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
Regexlet 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) -> BoolParameters
regexA Regex struct used to match the string
strA 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
Regexlet 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) -> BoolParameters
strA string waiting to match
regexA Regex struct used to match the string
Return Value
A bool value indicates whether the str is matched with the Regexp
-
An alias to self
Declaration
Swift
public var regex: Regex
View on GitHub
Regex Struct Reference