import Foundation
let pattern = ##"cat\s+.*\.xml(.*)\[Expert@Arcus-mgmt1\]#\s?"##
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = ##"""
cat namedQueries1.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<NamedQueriesConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="NamedCloudQueries.xsd">
<NamedQuery>
<Name>SC2</Name>
<RequestedInformation>AssignedIP</RequestedInformation>
<Constraint>
<FieldName>VdcName</FieldName>
<Operator>Equals</Operator>
<Value>SC2</Value>
</Constraint>
</NamedQuery>
<NamedQuery>
<Name>SC1</Name>
<RequestedInformation>AssignedIP</RequestedInformation>
<Constraint>
<FieldName>VdcName</FieldName>
<Operator>Equals</Operator>
<Value>SC1</Value>
</Constraint>
</NamedQuery>
</NamedQueriesConfiguration>[Expert@Arcus-mgmt1]#
"""##
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