0

I want to analyze a string by many different patterns for numbers, dates and other strings. So I have an array of patterns I want to check in that order.

let patterns = [... "\\d{6}", "\\d{4}", "\\d" ]  // to be extended :-)
let s = "IMG_123456_2006.10.03-13.52.59 Testfile_2009_5"

Starting with the first item in pattern I need a search in string s. If found, the string should be split in found parts e.g. "2006" and "2009" and the non matching parts. The remaining parts will be searched with the next pattern and so on. Assuming I already had the pattern defined for time/date in the middle which should be placed at the first item, the splitted string should look like:

"IMG_", "123456", "_", "2006.10.03-13.52.59", " Testfile_", "2009", "_", "5"

Can I use a build in functionality of regex.matches, or do I have to write everything by my own?

I already been able to find a match. But then I have to use the ranges to split the string and do it again and again for the remaining parts until no further matches are indicated. This will need a lot more calculations than I would expect using the results in match.numberOfRanges. Any small solutions available?

Peter71
  • 2,180
  • 4
  • 20
  • 33
  • Perhaps the answers in [this question](http://stackoverflow.com/questions/31368703/how-to-use-regex-with-swift) can inspire? Also, regex has this thing called capture groups. For example : `^([A-Za-z]+)_([0-9]+)_([0-9.]{10}-[0-9.]{8})[ ]+([A-Za-z0-9]+)_([0-9]+)_([0-9]+)$` to match that string. – LukStorms Oct 03 '16 at 11:47
  • I read it all, but I didn't get it. To find the first part is no problem, but I need the splitted data or all indeces where one part starts and the other ends. :-( – Peter71 Oct 04 '16 at 15:09

0 Answers0