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 providedpattern
. 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 providedString
as its pattern. If the pattern is invalid, this function callsfatalError()
. Hence, it is recommended that you useRegex.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 providedString
as its pattern. If the pattern is invalid, this function initializes anNSError
into the providedNSErrorPointer
.Regex.create()
is recommended, as it wraps this constructor and handles theNSErrorPointer
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 anNSError
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 providedreplacement
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 providedreplacement
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.