You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Regex ~= String
letvalue=try!Regex(pattern:"Hello [a-z]+ame")~="Hello Name"
// value is true
// String ~= Regex
letvalue="Hello Name"~=try!Regex(pattern:"Hello [a-z]+ame")
// value is true
Using regex matching with switch
switch"Hello I am on swift"{casetry!Regex(pattern:"Hello [a-z] am .*"):
// First
case try!Regex(pattern:".*"):
// Second
}
The first passing regex will be matched. In the example above, the first case
is matched.
String extensions
Replace a pattern with a string:
"This string is wrong".replacing(pattern:"w.*g", withString:"right")
// "This string is right"
CaptureResult
CaptureResult represent a captured string, it contains:
originalString the original string
startIndex the capture start index
endIndex the capture end index
range the range of the captured string
string the captured string
RegexOptions
RegexOptions optionset can be passed when initilaizing a Regex object. For
a discussion on the meaning of these flags, refer to GNU regex
documentation
MatchOptions
matches(_:options:) accepts the MatchOptions option set. For a discussion
on the meaning of these flags, refer to GNU regex
documentation
Installation
You can install Regex using Swift package manager (SPM) and carthage