-1

Why does the following sentence:

let potential = "Kaline bypassed the Minors to join the Tigers at 18. By 20"

pass through this RegEx if statement:

if (potential.matches("(?i)No.\\s?-?\\s?[A-Z0-9]") ||
potential.matches("(?i)N0.\\s?-?\\s?[A-Z0-9]")) 
{
   print("in"
}

extension String {
    func matches(_ regex: String) -> Bool {
        return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
    }
}
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

1 Answers1

1

Instead of checking using 2 patterns, you can use a character class [0o] and use a single pattern.

As already mentioned in the comments, you have to escape the dot to match it literally or else it will match any character.

(?i)N[0o]\\.\\s?-?\\s?[A-Z0-9]

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70