import Foundation
let pattern = ##"^((?<emoji_left>(?::\w*:|\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55]))\s)?(?<type>\w+)(?:\((?<scope>[^)]*)\))?!?:\s((?<emoji_center>(?::\w*:|\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55]))\s)?(?<subject>(?:(?!#).)*(?:(?!\s).))(?:\s(?<ticket>#(?<ticket_number>\w+)|\(#(?<ticket_number>\w+)\)))?(?:\s(?<emoji_right>(?::\w*:|\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55])))?$"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
chore(scope): test
chore(scope)!: test
chore!: test
chore!(scope)!: test
hello: :test: test #111 ✨
:start: test: 1wsdwqs #11111
✨ feat(unicode): ✨ adasas sfsd :das: #1d1w ✨
feat(unicode): add unicode suppdadass o # (#rtasa) ✨
:hello: test: test
:start: chore(scope): test
:start: chore(scope): test #123
:memo: docs: update README.md
:start: chore(scope): i have a word #123
:package: feat(parser-opts): extract parser-opts packages
:sparkles: feat(changelog): 添加中文标题
:sparkles: feat(changelog): add chinese title
✨ feat(unicode): add unicode support
😌 test(only): add unicode support
:sparkles: feat: hello world (#100)
"""##
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