import Foundation
let pattern = ##"[\#(/)_\-\=\!\:]"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
#/usr/bin/python3
from bisect import bisect_right
def binary_search(vector,x):
i = bisect_right(vector,x)
if i != len(vector)+1 and vector[i-1] == x:
return i-1
return -1
def is_prime(vector,number):
return 1 if binary_search(vector,number) != -1 else 0
def prime_vector(num):
ret = []
for a in range(2,num+1):
if a == 2 or a == 3 or a == 5 or a == 7 or a == 11:
ret.append(a)
elif a %2 == 0 or a%3 == 0 or a%5 == 0 or a%7==0 or a%11 == 0:
continue
else:
ret.append(a)
return ret
number = int(input())
prime = prime_vector(number)
ret = []
p = 0
while number:
if is_prime(prime,number):
ret.append(number)
break
elif number%prime[p] == 0:
ret.append(prime[p])
number = int(number / prime[p])
else:
p = p + 1
for a in range((len(ret))):
print(ret[a],end=" ")
"""##
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