import Foundation
let pattern = ##"^(?P<indentation>(?P<sp_or_tab>[ \t])*+)(?P<stmt>(?P<non_str_lit>(?:(?!\#)[^\"'\r\n])*?)(?:(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\[^\r\n ]|[^\"'\\\r\n])*?(?P=begin_quote))(?&non_str_lit))*?)(?P<whitespace>(?&sp_or_tab)*+)(?P<comment>(?:\#)[^\r\n]*+)?(?P<line_ending>\r|\n|\r\n)?$"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ####"""
print("Hello, world!") # this is a comment.
print("The quick \\brown\\\ndog\rfox\tjumps\t over #the# /lazy/ \"dog\"###") # and here's where the real comment starts
print("you can use Alt+0009 to enter a tab character!")
# The above line had no comments, so the <comment> group didn't participate in the match.
print("indented code") # with comment
print("never indent code like this, but it still matches anyway")# comment lacking preceding whitespace >:)
print("space indented code")
print("Missing end quote
print("unescaped \")
print("implicitly" " joined## " "string " "lit#erals") # a line can contain multiple string literals
print("the quick " # line 1
"brown fox " # line 2
"jumps over " # line 3
"the lazy dog") # line 4
print("The quick brown", animal, "jumps over the", adjective, animal2, sep=" ") "
print('Robert'); DROP TABLE `Students`; -- ')
"""####
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