Guest User

Untitled

a guest
Jan 8th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. data = [
  2. [
  3. "port" : "4711",
  4. "content" : "ServerAdmin webmaster@dummy-host.example.com
  5. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
  6. ServerName localhost
  7. ErrorLog "logs/localhost-error_log"
  8. CustomLog "logs/localhost-access_log" common"
  9. ]
  10. ]
  11.  
  12. let text = """
  13. <VirtualHost *:4711>
  14. ServerAdmin webmaster@dummy-host.example.com
  15. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
  16. ServerName localhost
  17. ErrorLog "logs/localhost-error_log"
  18. CustomLog "logs/localhost-access_log" common
  19. </VirtualHost>
  20.  
  21. <VirtualHost *:4711>
  22. ServerAdmin webmaster@dummy-host.example.com
  23. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs/4711Lab02/public"
  24. ServerName lab02.local
  25. ErrorLog "logs/lab02-error_log"
  26. CustomLog "logs/lab02-access_log" common
  27. </VirtualHost>
  28. """
  29. //let try2 = "<VirtualHost *?([^>]*)>(.*?)</VirtualHost>"
  30.  
  31. let try1 = "<VirtualHost ?([^>])>([\s\S\w\d]*)<\/VirtualHost>"
  32. let matched = matches(for: try1 , in: text)
  33. print(matched)
  34.  
  35.  
  36. }
  37.  
  38. func matches(for regex: String, in text: String) -> [String] {
  39. do {
  40. let regex = try NSRegularExpression(pattern: regex)
  41. let results = regex.matches(in: text,
  42. range: NSRange(text.startIndex..., in: text))
  43. return results.compactMap {
  44. Range($0.range, in: text).map { String(text[$0]) }
  45. }
  46. } catch let error {
  47. print("invalid regex: (error.localizedDescription)")
  48. return []
  49. }
  50.  
  51. data = [
  52. [
  53. "port" : "4711",
  54. "content" : "ServerAdmin webmaster@dummy-host.example.com
  55. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
  56. ServerName localhost
  57. ErrorLog "logs/localhost-error_log"
  58. CustomLog "logs/localhost-access_log" common"
  59. ]
  60. ]
  61.  
  62. <VirtualHost *:4711>
  63. ServerAdmin webmaster@dummy-host.example.com
  64. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
  65. ServerName localhost
  66. ErrorLog "logs/localhost-error_log"
  67. CustomLog "logs/localhost-access_log" common
  68. </VirtualHost>
  69.  
  70. let text = """
  71.  
  72. <VirtualHost *:4711>
  73. ServerAdmin webmaster@dummy-host.example.com
  74. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs"
  75. ServerName localhost
  76. ErrorLog "logs/localhost-error_log"
  77. CustomLog "logs/localhost-access_log" common
  78. </VirtualHost>
  79.  
  80. <VirtualHost *:4711>
  81. ServerAdmin webmaster@dummy-host.example.com
  82. DocumentRoot "/Applications/mampstack-5.5.31-0/apache2/htdocs/4711Lab02/public"
  83. ServerName lab02.local
  84. ErrorLog "logs/lab02-error_log"
  85. CustomLog "logs/lab02-access_log" common
  86. </VirtualHost>
  87.  
  88. """
  89.  
  90. let matched = matches(for: "~<VirtualHost ([^>]+)>(.+?)</VirtualHost>~is", in: text)
  91. print(matched)
  92.  
  93. func matches(for regex: String, in text: String) -> [String] {
  94. do {
  95. let regex = try NSRegularExpression(pattern: regex)
  96. let results = regex.matches(in: text,
  97. range: NSRange(text.startIndex..., in: text))
  98. return results.compactMap {
  99. Range($0.range, in: text).map { String(text[$0]) }
  100. }
  101. } catch let error {
  102. print("invalid regex: (error.localizedDescription)")
  103. return []
  104. }
  105. }
Add Comment
Please, Sign In to add comment