2

Is there a way to find out if a string matches certain Regular Expression? So far I have only found functions that are able to determine whether the string contains another string matching the regular expression but I want to find out a way to tell if the string MATCHES the regex.

extension String {
    func matches(_ regex: String) -> Bool {
        return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
    }
}

this is what I have tried

but when I have regex eg let regex = HIHOWAREYOU and i do "HIHOWAREYOUUUUUUU".matches(regex)

it returns true, but i need it to return false

this happens of course if I create a more complex regex such as [ -~]{1,10} I specify that the string must have from 1 - 10 characters but when it has 11 the function also returns true

Sulthan
  • 128,090
  • 22
  • 218
  • 270
eja08
  • 4,600
  • 3
  • 13
  • 19

0 Answers0