Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = [
- [
- "port" : "4711",
- "content" : "ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
- ServerName localhost
- ErrorLog "logs/localhost-error_log"
- CustomLog "logs/localhost-access_log" common"
- ]
- ]
- let text = """
- <VirtualHost *:4711>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
- ServerName localhost
- ErrorLog "logs/localhost-error_log"
- CustomLog "logs/localhost-access_log" common
- </VirtualHost>
- <VirtualHost *:4711>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs/4711Lab02/public"
- ServerName lab02.local
- ErrorLog "logs/lab02-error_log"
- CustomLog "logs/lab02-access_log" common
- </VirtualHost>
- """
- //let try2 = "<VirtualHost *?([^>]*)>(.*?)</VirtualHost>"
- let try1 = "<VirtualHost ?([^>])>([\s\S\w\d]*)<\/VirtualHost>"
- let matched = matches(for: try1 , in: text)
- print(matched)
- }
- func matches(for regex: String, in text: String) -> [String] {
- do {
- let regex = try NSRegularExpression(pattern: regex)
- let results = regex.matches(in: text,
- range: NSRange(text.startIndex..., in: text))
- return results.compactMap {
- Range($0.range, in: text).map { String(text[$0]) }
- }
- } catch let error {
- print("invalid regex: (error.localizedDescription)")
- return []
- }
- data = [
- [
- "port" : "4711",
- "content" : "ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
- ServerName localhost
- ErrorLog "logs/localhost-error_log"
- CustomLog "logs/localhost-access_log" common"
- ]
- ]
- <VirtualHost *:4711>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
- ServerName localhost
- ErrorLog "logs/localhost-error_log"
- CustomLog "logs/localhost-access_log" common
- </VirtualHost>
- let text = """
- <VirtualHost *:4711>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
- ServerName localhost
- ErrorLog "logs/localhost-error_log"
- CustomLog "logs/localhost-access_log" common
- </VirtualHost>
- <VirtualHost *:4711>
- ServerAdmin webmaster@dummy-host.example.com
- DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs/4711Lab02/public"
- ServerName lab02.local
- ErrorLog "logs/lab02-error_log"
- CustomLog "logs/lab02-access_log" common
- </VirtualHost>
- """
- let matched = matches(for: "~<VirtualHost ([^>]+)>(.+?)</VirtualHost>~is", in: text)
- print(matched)
- func matches(for regex: String, in text: String) -> [String] {
- do {
- let regex = try NSRegularExpression(pattern: regex)
- let results = regex.matches(in: text,
- range: NSRange(text.startIndex..., in: text))
- return results.compactMap {
- Range($0.range, in: text).map { String(text[$0]) }
- }
- } catch let error {
- print("invalid regex: (error.localizedDescription)")
- return []
- }
- }
Add Comment
Please, Sign In to add comment