import Foundation
let str = "Swift compiled programming language. Swift general-purpose"
let word = "Swift"
let pattern = "\\b\(word)\\b"
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matches(in: str, options: [], range: NSRange(location: 0, length: str.utf16.count))
let count = matches.count
print("The word '\(word)' occurs \(count) times")
} catch {
print("Error creating regular expression: \(error)")
}
/*
run:
The word 'Swift' occurs 2 times
*/