import Foundation
let pattern = #"<body>(.*?)<\/body>"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive, .dotMatchesLineSeparators])
let testString = #"""
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link href="http://localhost//themes/default/../dashboard/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="http://localhost//assets/cache/default_product_groups_product_groups_mod.css" media="screen" />
<link rel="shortcut icon" href="http://localhost//favicon.ico">
</head>
<body>
<p dir='rtl'>
<a target='_blank' href='https://zuz.mx/2e5y'>לרכישה מכאן במחיר 37.01$</a>
</p>
<input id="base_url_special" type="hidden" name="base_url_special" value="http://localhost//"/>
</body>
<script src="http://localhost//themes/default/js/jquery.min.js" type="text/javascript"></script>
<script src="http://localhost//assets/cache/default_fetchPG_product_groups_mod.js?_dt=1492617362" type="text/javascript"></script>
<script src="http://localhost//themes/default/../dashboard/js/bootstrap.min.js" type="text/javascript"></script>
</body>
</html>
"""#
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