Reputation: 25974
I am using:
var test = "pizzazz"
if let match = test.range(of: "g/zz", options: .regularExpression) {
print(test.substring(with: match))
}
This How do I set g for global search?
Upvotes: 0
Views: 910
Reputation: 2628
I'm not sure what should be the result of your example, but you are probably looking for one of these functions:
1)
func enumerateMatches(in string: String,
options: NSRegularExpression.MatchingOptions = [],
range: NSRange,
using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void)
https://developer.apple.com/reference/foundation/nsregularexpression/1409687-enumeratematches
2)
func matches(in string: String,
options: NSRegularExpression.MatchingOptions = [],
range: NSRange) -> [NSTextCheckingResult]
https://developer.apple.com/reference/foundation/nsregularexpression/1412446-matches
3)
func stringByReplacingMatches(in string: String,
options: NSRegularExpression.MatchingOptions = [],
range: NSRange,
withTemplate templ: String) -> String
4)
func replaceMatches(in string: NSMutableString,
options: NSRegularExpression.MatchingOptions = [],
range: NSRange,
withTemplate templ: String) -> Int
https://developer.apple.com/reference/foundation/nsregularexpression/1411139-replacematches
Upvotes: 2