Chris G.
Chris G.

Reputation: 25974

Regex g is for global search

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

Answers (1)

JMI
JMI

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

https://developer.apple.com/reference/foundation/nsregularexpression/1413740-stringbyreplacingmatches

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

Related Questions