import Foundation
let pattern = #"(%([^:]+):([^%]+)%)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
<!DOCTYPE html>/
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Testing the checkAndChangeToUrl-Function (Version 5)</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/resources/css/ext-all.css\" />
<script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/adapter/ext/ext-base-debug.js\"></script>
<script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/ext-all-debug.js\"></script>
<script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/locale/ext-lang-de.js\"></script>
<script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-resources/js/jquery/jquery-3.2.1.js\"></script>
<script type=\"text/javascript\">
function checkAndChangeToUrl(url, maxRetries, waitTime, waitFunc, startFunc, successFunc, errorFunc) {
console.log(\"ENTER checkAndChangeToUrl('\"+url+\"', \"+maxRetries+\", \"+waitTime+\")\");
var nRetry = 0;
var checkUrl = function () {
$.ajax({
url: url,
context: document.body,
crossDomain: true,
error: function(jqXHR, textStatus, errorThrown) {
console.error(textStatus + \";
\" + errorThrown);
nRetry++;
if (nRetry <= maxRetries) {
waitFunc.call(jqXHR, jqXHR);
console.log(\"Retry #\" + nRetry);
var timeout = (nRetry == 0 ? 0 : waitTime);
console.log(\"timeout = \" + timeout);
setTimeout(checkUrl, timeout);
if (nRetry === 1) {
console.log(\"**** Try and open the target application ****\");
// PROBLEM: parent[.parent].handleExternalEvent() existiert nicht...
startFunc.call(jqXHR, jqXHR);
}
} else {
errorFunc.call(jqXHR, jqXHR);
}
return;
},
success : function(data, textStatus, jqXHR) {
console.log(\"Success!\");
successFunc.call(jqXHR, jqXHR);
}
}).done(function() {
$( this ).addClass( \"done\" );
console.log(\"Done!\");
});
};
// Wir setzen das Ganze in Gang...
checkUrl();
}
function test_1(checkUrl, targetUrl, launchUrl) {
//var url = \"%param:checkUrl%\";
//var url = \"http://localhost:8080/RechercheServlet/rest/forcont/fxLIS?DOKID=1&VID=1&OP=RECEIVE\";
//var url = \"http://localhost:8085/gn\";
console.log(\"VOR checkAndChangeToUrl('\"+checkUrl+\"', 2, 5000) ...\");
var ret = checkAndChangeToUrl(checkUrl, 2, 2000, function(jqXHR) {
console.log(\"Waiting...\");
}, function(jqXHR) {
console.log(\"Start application...\");
$.ajax({
url: %param:launchUrl%, //\"/opusp-rest/client-action/execute/local/GIS_Launch\",
context: document.body,
crossDomain: false, //true, -- SYNCHRONER AUFRUF - SCHWERE SÜNDE...
headers: {
'Accept' : 'application/x-java-jnlp-file', /* application/jnlp */
'Content-Description' : 'File Transfer',
'Content-Disposition' : 'attachment; filename=GIS_Launch.jnlp'
},
error: function(jqXHR, textStatus, errorThrown) {
console.error(\"Fehler bei Aktion GIS_Launch:
\" + textStatus + \";
\" + errorThrown);
return;
},
success : function(data, textStatus, jqXHR) {
console.log(\"GIS_Launch: Success! JNLP = '\" + data + \"'\");
}
}).done(function() {
$( this ).addClass( \"done\" );
console.log(\"GIS_Launch: Done!\");
});
}, function(jqXHR) {
console.log(\"Success.\");
window.location.href = targetUrl;
}, function(jqXHR) {
console.log(\"Error: \" + jqXHR.statusText + \", jqXHR.state() = \" + jqXHR.state());
});
console.log(\"LEAVE test_1.\");
}
function performTest_1() {
test_1(
\"[__checkUrl__]\", // Parameter: checkUrl, z.B. \"http://localhost:8080/RechercheServlet\"
\"%param:targetUrl%\", // Parameter: targetUrl, z.B. \"http://localhost:8080/RechercheServlet/rest/forcont/fxLIS?DOKID=1&VID=1&OP=RECEIVE\"
\"[__launchUrl__]\" // Parameter: launchUrl, z.B. \"/opusp-rest/client-action/execute/local/GIS_Launch\"
)
}
$.when($.ready).then(function() {
performTest_1();
});
</script>
</head>
<body>
<a id=\"test_1\" onclick=\"performTest_1();\"><button type=\"button\">Perform Test #1</button></a>
</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