import Foundation
let pattern = #"^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
S43 1QQ
HP22 4EL
SN13 8NF
LA14 1JH
WV14 8GW
LS17 8XU
CH1 3AX
L31 8AR
RG14 6SY
AL7 1AU
M27 0NG
SA13 3UU
OL16 4TG
B43 5PY
PR4 3FN
GY6 8NQ
CF83 1TJ
IV31 6DB
WC1H 0AA
LS17 7LG
S25 2NT
PO22 9LH
NR23 1BP
SL2 3HR
PO7 5TP
PE8 5LL
DA1 4TT
TA2 8AQ
TS24 0HS
WV6 0PZ
PE7 3TX
BS24 7NF
NR12 8PL
BN13 2TG
LL13 7JF
SO51 7AE
BT48 6UE
SM6 8BF
WA8 9PH
HR1 4JR
TN1 1YJ
WS2 9JT
SL1 1PG
RH4 2WS
DY5 3TG
CM22 6XT
LN5 8HX
OX14 1PJ
BL0 0LR
TW17 9NL
G60 5NH
SY23 3RH
NG18 5JZ
CF44 7ES
YO17 8RT
WA13 9HZ
LL47 6TF
BT45 7HA
HP18 0PH
BD19 4BU
CO7 8AE
WN2 3NF
EH3 9SU
HU5 4AY
G76 8ND
BN7 2FH
TN3 0BE
BN26 5SJ
BA3 2PG
DH1 2TD
BH14 0LT
BL3 1QB
TN4 9XX
LL30 2YQ
SE6 9SG
B61 0LU
TA20 2SZ
NP4 5SA
YO30 4UN
YO30 2AY
NN13 5JX
PO17 5BE
BS31 2QG
GU7 9GR
CV37 9XY
DH2 2DA
BH9 3AW
PE28 2GE
IV30 8LS
CV13 0LF
PO9 2RB
NR2 3HS
TS18 2XB
LL18 4LU
LA9 4SX
PL9 9RA
HR4 7PL
L12 0NL
DN9 2NH
IV36 2TF
ML4 3NW
PE34 3FH
W1U 6RB
BS21 7XG
M8 4QU
DD2 5RH
BT78 3AJ
TN16 1EN
RM6 5LU
GY7 9RL
CW11 1AL
GL54 5RG
S5 7LB
ST5 5AT
S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
BT13 3PU
CF39 9LR
KY2 6PU
S61 2UF
PA16 7BW
DY12 2JQ
SK13 6NA
OX12 9ZE
B44 9ST
NE47 7EA
PE10 0HE
SM6 9ER
ME10 3ST
DY10 4QQ
CV37 0UN
PE32 1QJ
CM7 9LP
AL4 9UJ
M33 2NA
TN15 0HR
LS12 1HZ
KT2 5PA
SA4 8PH
LS25 2EQ
DE24 0PS
OL12 8DR
BT14 8HU
KA19 7AJ
NP7 8RR
SE3 0UX
RG26 3EA
NR30 5RE
KT18 5BU
CF5 5JG
BA22 7ES
UB5 5LY
SN1 4JQ
WA5 4LA
DN20 0HE
SS7 1SS
L13 3DE
KT1 3QZ
G33 1SS
DN14 9PW
N1 2SB
SS1 3NJ
CF23 9LX
EX14 1PZ
CF48 1NN
EX20 1RX
B27 7QA
SO41 0WR
CO7 6UP
EH54 6RX
SW18 1RX
SN9 5AX
RG4 9RS
KT9 1SE
CM7 3YN
BB4 9SA
CR8 5DH
EN2 6HE
RH19 2PG
NG34 7TL
CV21 1JH
TQ3 1QX
ST14 8RP
DA7 4PE
TN29 9DU
HU16 4BT
N8 0LY
SK6 1AW
IP15 5JW
SN2 2FR
SE16 2PQ
ST13 6LE
SA1 8DT
LS5 3DN
B65 8DN
TF6 5DP
B66 3LE
L39 3RD
RM10 9YU
NN6 9JY
CF14 5WF
LS6 3HD
PL2 3NW
PR4 6RL
DL8 4AN
EH4 2RQ
SL1 7HT
LL22 8DH
N21 1BL
BA12 0RB
TD5 7LN
DL14 9AU
RG9 5QX
SA32 8PF
SO30 2LP
HP23 6EH
PL25 5JQ
G75 8WA
TS6 7EW
BT14 6BZ
WS15 1BA
WF1 9RT
DN15 6DW
SG6 1UG
AB53 5PJ
DN33 2PF
IP3 0NT
NP20 3FX
N15 4LJ
WA15 7QB
HU6 7EG
CV21 1SQ
TR26 2PY
LA14 1SR
OX7 3HR
W10 4UF
S12 2QZ
BA22 9TB
CB23 7GD
"""#
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