Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. let testString = """
  2. The cat
  3. sat on
  4. the mat
  5. """
  6.  
  7. public struct RegularExpression: ExpressibleByStringLiteral {
  8. public let pattern: String
  9. private let regularExpression: NSRegularExpression
  10.  
  11. init(_ pattern: String) {
  12. self.pattern = pattern
  13. do {
  14. self.regularExpression = try NSRegularExpression(pattern: pattern)
  15. } catch {
  16. preconditionFailure("Illegal regular expression pattern: \(pattern).")
  17. }
  18. }
  19.  
  20. public init(stringLiteral value: String) {
  21. self.init(value)
  22.  
  23. }
  24.  
  25. public func matches(_ string: String) -> Bool {
  26. return self.regularExpression.firstMatch(in: string, options: [], range: NSRange(string.startIndex..., in: string)) != nil
  27. }
  28. }
  29.  
  30.  
  31. let regularExpression: RegularExpression = "the"
  32. regularExpression.matches(testString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement