-1

I am using the Swift Playground to experiment with NSRegularExpression and I do not get what I would like to. Here is my code:

import UIKit

let str = "qwun782h218gs634  AbCd56EfGh7ZsDe985\nXzSr519UkGe9823SdFg91nui uihiheg875d dss77ds",
patn = "(([A-Z][a-z]){2}([0-9]+)){3}",
rgx  = try! NSRegularExpression(pattern: patn, options: .AnchorsMatchLines)

print("Start with:\(str)")
let strNS = str as NSString

rgx.enumerateMatchesInString(str, options: .ReportCompletion,
                                          range: NSMakeRange(0,str.utf8.count),
                                          usingBlock: { (result, _, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                                            if let _ = result?.range.location {
                                                var n = 0,
                                                theStr = strNS.substringWithRange(NSMakeRange((result?.rangeAtIndex(n).location)!,
                                                    (result?.rangeAtIndex(n).length)!))
                                                print("xpr [\(n)]: \(theStr)")
                                                n = 1
                                                theStr = strNS.substringWithRange(NSMakeRange((result?.rangeAtIndex(n).location)!,
                                                    (result?.rangeAtIndex(n).length)!))
                                                print("xpr [\(n)]: \(theStr)")
                                                n = 2
                                                theStr = strNS.substringWithRange(NSMakeRange((result?.rangeAtIndex(n).location)!,
                                                    (result?.rangeAtIndex(n).length)!))
                                                print("xpr [\(n)]: \(theStr)")
                                                n = 3
                                                theStr = strNS.substringWithRange(NSMakeRange((result?.rangeAtIndex(n).location)!,
                                                    (result?.rangeAtIndex(n).length)!))
                                                print("xpr [\(n)]: \(theStr)")
                                           }
})

This is what I get in the result console:

Start with:qwun782h218gs634  AbCd56EfGh7ZsDe985
XzSr519UkGe9823SdFg91nui uihiheg875d dss77ds

xpr [0]: AbCd56EfGh7ZsDe985
xpr [1]: ZsDe985
xpr [2]: De
xpr [3]: 985
xpr [0]: XzSr519UkGe9823SdFg91
xpr [1]: SdFg91
xpr [2]: Fg
xpr [3]: 91

Finally here is what I am interested in:

xpr: 56
xpr: 7
xpr: 985
xpr: 519
xpr: 9823
xpr: 91

In other words I want to capture all the groups of numbers.

In this example, I could of course change the shape of my regular expression, get rid of the {2} and {3} and make it work (by extending it). But this is not what I want and this is the purpose of my question. How can I keep this compacted form of the regular expression, while being able to capture what I want?

If instead of {2} and {3}, I had {12} and {37}; nobody would like to extend the expression :)

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

1

Actually the regex \\d+ (one numeric character or more) is sufficient.

let patn = "\\d+"
let str = "AbCd56EfGh7ZsDe985\nXzSr519UkGe9823SdFg91",
rgx  = try! NSRegularExpression(pattern: patn, options: [])

print("Start with:\(str)")

rgx.enumerateMatchesInString(str, options: [], range: NSRange(location:0, length:str.characters.count)) { (result, _, _) in
  if result != nil { print("xpr: \((str as NSString).substringWithRange(result!.range))") }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • You are right about the "numeric character or more", but that does not answer my question. I am not looking for sequence of numeric characters in general. I am looking for sequence of numbers inside a pattern described by this regular expression: `"(([A-Z][a-z]){2}([0-9]+)){3}"` which is a very different issue. – Michel Apr 19 '16 at 06:34
  • You wrote *Finally here is what I am interested in* and the simple regex does that exactly ;-) – vadian Apr 19 '16 at 06:38
  • After rereading my post, I realized that your interpretation of my question was after all correct. I therefore edited the post to make the question clearer and unambiguous. Sorry for that misleading. – Michel Apr 19 '16 at 06:46