import Foundation
let pattern = #"^(?:([0-1])|(2)|(3))(?(1)[0-9])(?(2)(?:([0-8])|(9)))(?(3)[0-1])\/(?:(01|03|05|07|09|11)|(04|06|08|10|12)|(02))(?(3)(?(8)(?!)))\/(?:(04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)|(\d\d))(?:(00)|(04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)|\d{2})(?(2)(?(5)(?(8)(?(11)(?(9)|(?!))|(?(12)|(?!))))))"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .allowCommentsAndWhitespace])
let testString = #"""
29/02/1200
29/02/1400
01/01/1999
02/01/1999
03/01/1999
04/01/1999
05/01/1999
06/01/1999
07/01/1999
08/01/1999
09/01/1999
10/01/1999
11/01/1999
12/01/1999
13/01/1999
14/01/1999
15/01/1999
16/01/1999
17/01/1999
18/01/1999
19/01/1999
20/01/1999
21/01/1999
22/01/1999
23/01/1999
24/01/1999
25/01/1999
26/01/1999
27/01/1999
28/01/1999
29/01/1999
30/01/1999
31/01/1999
01/02/1999
02/02/1999
03/02/1999
04/02/1999
05/02/1999
06/02/1999
07/02/1999
08/02/1999
09/02/1999
10/02/1999
11/02/1999
12/02/1999
13/02/1999
14/02/1999
15/02/1999
16/02/1999
17/02/1999
18/02/1999
19/02/1999
20/02/1999
21/02/1999
22/02/1999
23/02/1999
24/02/1999
25/02/1999
26/02/1999
27/02/1999
28/02/1999
01/03/1999
02/03/1999
03/03/1999
04/03/1999
05/03/1999
06/03/1999
07/03/1999
08/03/1999
09/03/1999
10/03/1999
11/03/1999
12/03/1999
13/03/1999
14/03/1999
15/03/1999
16/03/1999
17/03/1999
18/03/1999
19/03/1999
20/03/1999
21/03/1999
22/03/1999
23/03/1999
24/03/1999
25/03/1999
26/03/1999
27/03/1999
28/03/1999
29/03/1999
30/03/1999
31/03/1999
01/04/1999
02/04/1999
03/04/1999
04/04/1999
05/04/1999
06/04/1999
07/04/1999
08/04/1999
09/04/1999
10/04/1999
11/04/1999
12/04/1999
13/04/1999
14/04/1999
15/04/1999
16/04/1999
17/04/1999
18/04/1999
19/04/1999
20/04/1999
21/04/1999
22/04/1999
23/04/1999
24/04/1999
25/04/1999
26/04/1999
27/04/1999
28/04/1999
29/04/1999
30/04/1999
01/05/1999
02/05/1999
03/05/1999
04/05/1999
05/05/1999
06/05/1999
07/05/1999
08/05/1999
09/05/1999
10/05/1999
11/05/1999
12/05/1999
13/05/1999
14/05/1999
15/05/1999
16/05/1999
17/05/1999
18/05/1999
19/05/1999
20/05/1999
21/05/1999
22/05/1999
23/05/1999
24/05/1999
25/05/1999
26/05/1999
27/05/1999
28/05/1999
29/05/1999
30/05/1999
31/05/1999
01/06/1999
02/06/1999
03/06/1999
04/06/1999
05/06/1999
06/06/1999
07/06/1999
08/06/1999
09/06/1999
10/06/1999
11/06/1999
12/06/1999
13/06/1999
14/06/1999
15/06/1999
16/06/1999
17/06/1999
18/06/1999
19/06/1999
20/06/1999
21/06/1999
22/06/1999
23/06/1999
24/06/1999
25/06/1999
26/06/1999
27/06/1999
28/06/1999
29/06/1999
30/06/1999
01/07/1999
02/07/1999
03/07/1999
04/07/1999
05/07/1999
06/07/1999
07/07/1999
08/07/1999
09/07/1999
10/07/1999
11/07/1999
12/07/1999
13/07/1999
14/07/1999
15/07/1999
16/07/1999
17/07/1999
18/07/1999
19/07/1999
20/07/1999
21/07/1999
22/07/1999
23/07/1999
24/07/1999
25/07/1999
26/07/1999
27/07/1999
28/07/1999
29/07/1999
30/07/1999
31/07/1999
01/08/1999
02/08/1999
03/08/1999
04/08/1999
05/08/1999
06/08/1999
07/08/1999
08/08/1999
09/08/1999
10/08/1999
11/08/1999
12/08/1999
13/08/1999
14/08/1999
15/08/1999
16/08/1999
17/08/1999
18/08/1999
19/08/1999
20/08/1999
21/08/1999
22/08/1999
23/08/1999
24/08/1999
25/08/1999
26/08/1999
27/08/1999
28/08/1999
29/08/1999
30/08/1999
31/08/1999
01/09/1999
02/09/1999
03/09/1999
04/09/1999
05/09/1999
06/09/1999
07/09/1999
08/09/1999
09/09/1999
10/09/1999
11/09/1999
12/09/1999
13/09/1999
14/09/1999
15/09/1999
16/09/1999
17/09/1999
18/09/1999
19/09/1999
20/09/1999
21/09/1999
22/09/1999
23/09/1999
24/09/1999
25/09/1999
26/09/1999
27/09/1999
28/09/1999
29/09/1999
30/09/1999
01/10/1999
02/10/1999
03/10/1999
04/10/1999
05/10/1999
06/10/1999
07/10/1999
08/10/1999
09/10/1999
10/10/1999
11/10/1999
12/10/1999
13/10/1999
14/10/1999
15/10/1999
16/10/1999
17/10/1999
18/10/1999
19/10/1999
20/10/1999
21/10/1999
22/10/1999
23/10/1999
24/10/1999
25/10/1999
26/10/1999
27/10/1999
28/10/1999
29/10/1999
30/10/1999
31/10/1999
01/11/1999
02/11/1999
03/11/1999
04/11/1999
05/11/1999
06/11/1999
07/11/1999
08/11/1999
09/11/1999
10/11/1999
11/11/1999
12/11/1999
13/11/1999
14/11/1999
15/11/1999
16/11/1999
17/11/1999
18/11/1999
19/11/1999
20/11/1999
21/11/1999
22/11/1999
23/11/1999
24/11/1999
25/11/1999
26/11/1999
27/11/1999
28/11/1999
29/11/1999
30/11/1999
01/12/1999
02/12/1999
03/12/1999
04/12/1999
05/12/1999
06/12/1999
07/12/1999
08/12/1999
09/12/1999
10/12/1999
11/12/1999
12/12/1999
13/12/1999
14/12/1999
15/12/1999
16/12/1999
17/12/1999
18/12/1999
19/12/1999
20/12/1999
21/12/1999
22/12/1999
23/12/1999
24/12/1999
25/12/1999
26/12/1999
27/12/1999
28/12/1999
29/12/1999
30/12/1999
31/12/1999
01/01/2000
02/01/2000
03/01/2000
04/01/2000
05/01/2000
06/01/2000
07/01/2000
08/01/2000
09/01/2000
10/01/2000
11/01/2000
12/01/2000
13/01/2000
14/01/2000
15/01/2000
16/01/2000
17/01/2000
18/01/2000
19/01/2000
20/01/2000
21/01/2000
22/01/2000
23/01/2000
24/01/2000
25/01/2000
26/01/2000
27/01/2000
28/01/2000
29/01/2000
30/01/2000
31/01/2000
01/02/2000
02/02/2000
03/02/2000
04/02/2000
05/02/2000
06/02/2000
07/02/2000
08/02/2000
09/02/2000
10/02/2000
11/02/2000
12/02/2000
13/02/2000
14/02/2000
15/02/2000
16/02/2000
17/02/2000
18/02/2000
19/02/2000
20/02/2000
21/02/2000
22/02/2000
23/02/2000
24/02/2000
25/02/2000
26/02/2000
27/02/2000
28/02/2000
29/02/2000
01/03/2000
02/03/2000
03/03/2000
04/03/2000
05/03/2000
06/03/2000
07/03/2000
08/03/2000
09/03/2000
10/03/2000
11/03/2000
12/03/2000
13/03/2000
14/03/2000
15/03/2000
16/03/2000
17/03/2000
18/03/2000
19/03/2000
20/03/2000
21/03/2000
22/03/2000
23/03/2000
24/03/2000
25/03/2000
26/03/2000
27/03/2000
28/03/2000
29/03/2000
30/03/2000
31/03/2000
01/04/2000
02/04/2000
03/04/2000
04/04/2000
05/04/2000
06/04/2000
07/04/2000
08/04/2000
09/04/2000
10/04/2000
11/04/2000
12/04/2000
13/04/2000
14/04/2000
15/04/2000
16/04/2000
17/04/2000
18/04/2000
19/04/2000
20/04/2000
21/04/2000
22/04/2000
23/04/2000
24/04/2000
25/04/2000
26/04/2000
27/04/2000
28/04/2000
29/04/2000
30/04/2000
01/05/2000
02/05/2000
03/05/2000
04/05/2000
05/05/2000
06/05/2000
07/05/2000
08/05/2000
09/05/2000
10/05/2000
11/05/2000
12/05/2000
13/05/2000
14/05/2000
15/05/2000
16/05/2000
17/05/2000
18/05/2000
19/05/2000
20/05/2000
21/05/2000
22/05/2000
23/05/2000
24/05/2000
25/05/2000
26/05/2000
27/05/2000
28/05/2000
29/05/2000
30/05/2000
31/05/2000
01/06/2000
02/06/2000
03/06/2000
04/06/2000
05/06/2000
06/06/2000
07/06/2000
08/06/2000
09/06/2000
10/06/2000
11/06/2000
12/06/2000
13/06/2000
14/06/2000
15/06/2000
16/06/2000
17/06/2000
18/06/2000
19/06/2000
20/06/2000
21/06/2000
22/06/2000
23/06/2000
24/06/2000
25/06/2000
26/06/2000
27/06/2000
28/06/2000
29/06/2000
30/06/2000
01/07/2000
02/07/2000
03/07/2000
04/07/2000
05/07/2000
06/07/2000
07/07/2000
08/07/2000
09/07/2000
10/07/2000
11/07/2000
12/07/2000
13/07/2000
14/07/2000
15/07/2000
16/07/2000
17/07/2000
18/07/2000
19/07/2000
20/07/2000
21/07/2000
22/07/2000
23/07/2000
24/07/2000
25/07/2000
26/07/2000
27/07/2000
28/07/2000
29/07/2000
30/07/2000
31/07/2000
01/08/2000
02/08/2000
03/08/2000
04/08/2000
05/08/2000
06/08/2000
07/08/2000
08/08/2000
09/08/2000
10/08/2000
11/08/2000
12/08/2000
13/08/2000
14/08/2000
15/08/2000
16/08/2000
17/08/2000
18/08/2000
19/08/2000
20/08/2000
21/08/2000
22/08/2000
23/08/2000
24/08/2000
25/08/2000
26/08/2000
27/08/2000
28/08/2000
29/08/2000
30/08/2000
31/08/2000
01/09/2000
02/09/2000
03/09/2000
04/09/2000
05/09/2000
06/09/2000
07/09/2000
08/09/2000
09/09/2000
10/09/2000
11/09/2000
12/09/2000
13/09/2000
14/09/2000
15/09/2000
16/09/2000
17/09/2000
18/09/2000
19/09/2000
20/09/2000
21/09/2000
22/09/2000
23/09/2000
24/09/2000
25/09/2000
26/09/2000
27/09/2000
28/09/2000
29/09/2000
30/09/2000
01/10/2000
02/10/2000
03/10/2000
04/10/2000
05/10/2000
06/10/2000
07/10/2000
08/10/2000
09/10/2000
10/10/2000
11/10/2000
12/10/2000
13/10/2000
14/10/2000
15/10/2000
16/10/2000
17/10/2000
18/10/2000
19/10/2000
20/10/2000
21/10/2000
22/10/2000
23/10/2000
24/10/2000
25/10/2000
26/10/2000
27/10/2000
28/10/2000
29/10/2000
30/10/2000
31/10/2000
01/11/2000
02/11/2000
03/11/2000
04/11/2000
05/11/2000
06/11/2000
07/11/2000
08/11/2000
09/11/2000
10/11/2000
11/11/2000
12/11/2000
13/11/2000
14/11/2000
15/11/2000
16/11/2000
17/11/2000
18/11/2000
19/11/2000
20/11/2000
21/11/2000
22/11/2000
23/11/2000
24/11/2000
25/11/2000
26/11/2000
27/11/2000
28/11/2000
29/11/2000
30/11/2000
01/12/2000
02/12/2000
03/12/2000
04/12/2000
05/12/2000
06/12/2000
07/12/2000
08/12/2000
09/12/2000
10/12/2000
11/12/2000
12/12/2000
13/12/2000
14/12/2000
15/12/2000
16/12/2000
17/12/2000
18/12/2000
19/12/2000
20/12/2000
21/12/2000
22/12/2000
23/12/2000
24/12/2000
25/12/2000
26/12/2000
27/12/2000
28/12/2000
29/12/2000
30/12/2000
31/12/2000
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression