0

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?

Chris G.
  • 18,319
  • 35
  • 125
  • 223

1 Answers1

2

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

JMI
  • 2,252
  • 10
  • 25