import Foundation
let pattern = #"(?<error>.+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
/P <</MCID 0 >>BDC
1 g
/GS0 gs
203.42 454.87 148.01 76.464 re
f*
EMC
/Artifact BMC
q
51.36 206.66 509.64 530.38 re
W* n
0.749 g
50.88 375.53 509.76 10.584 re
f*
Q
q
51.36 206.66 509.64 530.38 re
W* n
0.749 g
50.88 302.45 509.76 10.56 re
f*
EMC
Q
/P <</MCID 1 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/GS1 gs
/TT0 6.48 Tf
67.08 622.42 Td
[(D)3.997 (ate:)6.003 ( )]TJ
ET
EMC
Q
/P <</MCID 2 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
110.06 622.42 Td
[(4)7.006 (/1)3.997 (8)6.991 (/2)4.012 (0)6.991 (2)7.006 (4)]TJ
ET
EMC
Q
/P <</MCID 3 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
52.2 604.54 Td
[(C)-2.994 (u)5.988 (st)4.012 (o)7.994 (mer Name:)6.003 ( )]TJ
ET
EMC
Q
/P <</MCID 4 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
160.82 604.54 Td
[(V)-6.003 (er)-3.997 (sal)7.006 (i)7.994 (f)8.997 (t)]TJ
ET
EMC
Q
/P <</MCID 5 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
358.99 604.54 Td
[(A)5 (d)6.003 (d)6.003 (re)-4.012 (ss)4.012 (:)]TJ
ET
EMC
Q
/P <</MCID 6 >>BDC
q
388.99 593.86 59.424 25.68 re
W* n
BT
0 g
/TT0 6.48 Tf
389.83 604.78 Td
[(7)7.006 (6)6.991 (0)7.006 (1)6.991 ( )4.012 (I)-6.003 (mp)8.997 (er)-3.997 (i)7.994 (al)5 ( )3.997 (D)4.012 (ri)5 (v)7.994 (e )]TJ
ET
Q
q
388.99 593.86 59.424 25.68 re
W* n
BT
0 g
/TT0 6.48 Tf
389.83 596.26 Td
[(Wac)-2.994 (o)7.994 (,)8.997 ( )3.997 (T)6.003 (X )3.997 (7)7.006 (6)7.006 (7)6.991 (1)7.006 (2)]TJ
ET
EMC
Q
/P <</MCID 7 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
52.2 556.18 Td
[(C)-2.994 (u)5.988 (st)4.012 (o)7.994 (mer Par)-3.997 (t )5 (Nu)3.997 (mb)8.997 (er)-3.997 (:)8.997 ( )]TJ
ET
EMC
Q
/P <</MCID 8 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
160.94 556.18 Td
[(2)7.006 (1)6.991 (6)7.006 (7)6.991 (8)7.006 (-)-7.994 (1)]TJ
ET
EMC
Q
/P <</MCID 9 >>BDC
q
51.36 206.66 509.64 530.38 re
W* n
BT
0 g
/TT0 6.48 Tf
-0.0454 Tc 226.73 556.18 Td
[(1007922-)-15 (1)]
[(h)1.000 ]
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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