import Foundation
let pattern = #"href\W+=\W+['|\"]([http|https]+://.+)['|\"]"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页加载中...</title>
<style type="text/css">
.sk-three-bounce {
margin: 100px auto;
width: 80px;
text-align: center;
}
.sk-three-bounce .sk-child {
width: 20px;
height: 20px;
background-color: rgba(52, 214, 27, 0.76);
border-radius: 100%;
display: inline-block;
-webkit-animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
}
.sk-three-bounce .sk-bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.sk-three-bounce .sk-bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes sk-three-bounce {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1);
transform: scale(1);
}
}
@keyframes sk-three-bounce {
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1);
transform: scale(1);
}
}
</style>
</head>
<body>
<div class="sk-three-bounce">
<div class="sk-child sk-bounce1"></div>
<div class="sk-child sk-bounce2"></div>
<div class="sk-child sk-bounce3"></div>
</div>
</body>
<script type="text/javascript">
document.title = '网页加载中...';
var ua = navigator.userAgent;
if (ua.indexOf('MicroMessenger') > 0 ||ua.indexOf('QQ')>0||ua.indexOf('qq')>0) {
var noreferrer = true;
setTimeout(skip,1000)
}
else {
top.location.href = 'https://xw.qq.com';
};
function skip() {
if (top != window || !document.body) {
top.location.href = 'http://zruhdc.ddccee.top/travel_hot_gonglve_59721879943236';
}
else {
var a = document.createElement('a');
a.href = 'http://zruhdc.ddccee.top/travel_hot_gonglve_59721879943236';
if (noreferrer)
a.rel = 'noreferrer';
a.click();
}
}
</script>
</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