import Foundation
let pattern = #"(?:"?[a-z0-9_\-]{0,256}(?:key|secret|token)[a-z0-9_\-]{0,256}"?\s{0,10}(?::|=)\s{0,10}\"?(?!null|true|false)([a-z0-9+_:\.\-\/]{1,1024})|"Authorization":"[a-z0-9+:_\-\/]{1,1024}\s(.{1,1024}?(?<!\\)(?=")))"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
{"secret":"asdfffasdf","headers":{"uri_1":"https://www.api.example.com/api/?count=433&apiKey=DD238B9E4369248330EFBB58D6B1431AFB03C8E1D&debug=true","uri_2":"https://www.api.example.com/api/?secret=5ZQD9jhp51qVE5199xU4zj2X2wSPGwNpDub2CiSJO3i","uri_3":"https://www.api.example.com/api/?token=CE362359AAFCB1F8C5BAF4692FD67C18&debug=true","uri_4":"https://www.api.example.com/api/?secret_token=4DA9155E26A75C6D83E208FF518EAFE4&debug=true","uri_5":"https://www.api.example.com/api/?count=433&key=NeRPfka6TETPfHvNoLP4WPBxhhsrOzOh9HyC&debug=true","uri_6":"https://www.api.example.com/api/?apitoken=jq3PGKQDhKjJVo33hRVPK36q0r9-asdf_asdf","uri_7":"https://www.api.example.com/api/?api_key=7119971F47DE25C366F9AB506A1BFD&debug=true","uri_8":"https://www.api.example.com/api/?secret-key=93326A9B41C3DF94BED6E4C9DC524A4F6","uri_9":"https://www.api.example.com/api/?client_secret=6C844DE60C4104936541D4EAB32404DA&debug=true","uri_10":"https://www.api.example.com/api/?count=433&list=daisy&test=true&api_token=lYb6y4GYvFjmW0F8bGAXtcogmkRGa3hkgph530&debug=true","uri_11":"https://www.api.example.com/api/?count=433&api_secret=lYb6y4GYvFjmW0F8bGAXtcogmkRGa3hkgph530&debug=true","uri_12":"https://www.api.example.com/api/?token-pin=8433","uri_13":"https://www.api.example.com/api/?key=4343","uri_14":"https://www.api.example.com/api/?secret-pin=32211190332933","auth_0":{"Authorization":"Digest username=username, asfsfsf=\"dsffadf\",\nasfsfsf=\"dsffadf\",\nasfsfsf=\"dsffadf\",\nasdfsf=asdfsdf"},"auth_1":{"Authorization":"Bearer dasd/4rV4MjOdjA3pu9rJ5qc9RKuCoAO8UaxuWUGXUtuzRJKdRTvKMVe3dJ9FN1SyF9n=="},"auth_2":{"Authorization":"App D4D0BB1B2F12B5E97405C764CA45F"},"auth_3":{"Authorization":"IPSO dasd+F51B63334AD3520894712D15D8F1105ED3DD"},"auth_4":{"Authorization":"Key hJdilwrzM9616MJsDGBiK4qjeJFYB0zmHPxYNUrn8D54ycAN7gwedqHt0UiCWTb"},"auth_5":{"Authorization":"Digest username=username, oauth=\"hhasjjjd+ddad\",\nnonce=\"dsffadf\",\nhash=\"ef05bc-89c2\",\nclient=myapp"},"auth_6":{"Authorization":"Digest username=\"6551156d‑569c‑4b7d‑945f‑310ff10943c5\", realm=\"bob@contoso.com\", qop=auth, algorithm=MD5-sess, uri=\"sip:bob@contoso.com;gruu;opaque=app:conf:focus:id:854T0R7G\", nonce=\"h8A4ZW22ygGZozIIGZcb43waVMEM6Gq\", nc=1, cnonce=\"\", opaque=\"0C1D4536\", response=\"b4543cd4d6a923b4ab4fd4583af48f0e\""},"auth_7":{"Authorization":"Digest username=\"6551156d‑569c‑4b7d‑945f‑310ff10943c5\",\n realm=\"bob@contoso.com\",\n qop=auth,\n algorithm=MD5-sess,\n uri=\"sip:bob@contoso.com;gruu;opaque=app:conf:focus:id:854T0R7G\",\n nonce=\"h8A4ZW22ygGZozIIGZcb43waVMEM6Gq\",\n nc=1, cnonce=\"\",\n opaque=\"0C1D4536\",\n response=\"b4543cd4d6a923b4ab4fd4583af48f0e\""},"apiId":null,"apiId_2":null,"apiKey4":12345689994,"apiKey5":"123344555",apiKey":null,"apiKey2":null,"apiKey3":"null"},"multiValueHeaders":{"Accept":["*/*"],"Accept-Encoding":["gzip, deflate, br"],"Cache-Control":["no-cache"],"Host":["myapi.api.example.com"],"Postman-Token":["86071fc6-35da-4ff5-8eb5-d44ce06e3eed"],"User-Agent":["PostmanRuntime/7.31.1"],"X-Amzn-Trace-Id":["Root=1-6428887b-15fa842f16c1eeaf5d812345"],"X-Forwarded-For":["10.61.41.39"],"X-Forwarded-Port":["443"],"X-Forwarded-Proto":["https"],"Client-Secret":["451234567890123456","90123456789012345","e3bc8a2051bae92cc32",8123456789012345678,"ed1234567890ca1234"],"Client-Token":["awbu+d4Tey28-32ntRW3X",82340514553298753123456,"hw_538291-ec0a91-123456"]}}
"""#
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