import Foundation
// WARNING: You included a flag that Swift doesn't support: U
// When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
// As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
let pattern = #"(<script.*-(1|2)\.gif.*<\/script>)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = #"""
<script type="text/javascript">ViewGifImage(this, 'sitedb/log/QA/General/SfpBCTuningCheck/gfx/qa_2023-02-01-212831-1.gif','sitedb/log/QA/General/SfpBCTuningCheck/gfx/qa_2023-02-01-212831-1.pix','322','242','ReflectionResultPlot','Frequency in MHz','0','Reflection Factor |r|','0','1','xffffff x000000 x000000 xff0000 x0000ff x00df00 xff00ff xff7f00','2','158','1','Reflection 0 Degrees','158','1','Reflection 90 Degrees','','','','','','','','','')
</script>
<script type="text/javascript">ViewGifImage(this, 'sitedb/log/QA/General/SfpBCTuningCheck/gfx/qa_2023-02-01-212831-2.gif','sitedb/log/QA/General/SfpBCTuningCheck/gfx/qa_2023-02-01-212831-2.pix','322','242','TransmissionResultPlot','Frequency in MHz','0','Transmission |t| in db','0','1','xffffff x000000 x000000 xff0000 x0000ff x00df00 xff00ff xff7f00','2','158','1','Transmission 0 Degrees','158','1','Transmission 90 Degrees','','','','','','','','','')
</script>
"""#
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