-1

Following is my code:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let prospectiveText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)

        if prospectiveText.validateAsPerRegExpression(regExpression: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]{}#%^*+=_\\|~<>€£¥•-/:;()$&@\".,?!\'") == false {
            return false
        }

        return true
}

extension String {
    func validateAsPerRegExpression(regExpression: String) -> Bool {
        let floatExPredicate = NSPredicate(format:"SELF MATCHES %@", regExpression)
        return floatExPredicate.evaluate(with: self)
    }
}

Getting the following crash:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_INVALID_RANGE (string e, pattern ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]{}#%^*+=_\|~<>€£¥•-/:;()$&@".,?!', case 0, canon 0)'

I think i'm forming regular expression in wrong way, Any idea on the fix?

Actually my intension is to avoid user to enter any other language input from keyboard, but all the keys in normal keyboard. If there is any better way to do it, please let me know. Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Ashok
  • 5,585
  • 5
  • 52
  • 80
  • What are you trying to do? Your `regExpression` contains plenty of special regex characters like `[]` or `()` and if not treated properly, will give you that invalid regex pattern error – Code Different Apr 16 '17 at 14:52
  • Everything you're doing is wrong. Your `regExpression` is not a valid regular expression pattern. You are using NSPredicate incorrectly and unnecessarily. Start over. Delete this question and formulate a question explaining _what you really want to do_ and asking how to do it. – matt Apr 16 '17 at 15:44
  • Possible duplicate of [How to match hyphens with Regular Expression?](https://stackoverflow.com/questions/4068629/how-to-match-hyphens-with-regular-expression) – Wiktor Stribiżew Feb 26 '18 at 20:42

1 Answers1

1

The problem is your regex. You need to escape the regex identifiers, otherwise, the compiler tries to make a formula out of them and fail. If you want to accept only English characters with special characters, use this

[A-Za-z0-9[]{}#%\^*+=_\|~<>€£¥•-/:;()$&@\".,?!\']

This only matches one character at a time. If you want to match one or more characters i.e entire string, use a +(one or more match) or *(zero or more match) at the end of the expression.

[A-Za-z0-9[]{}#%\^*+=_\|~<>€£¥•-/:;()$&@\".,?!\']+

This will match one or more characters (non-empty string). For future, you can use this website Regexr, this will verify your regex for you.

Ravi Aggarwal
  • 159
  • 1
  • 1
  • 10
  • To avoid compilation errors, used the following: if prospectiveText.validateAsPerRegExpression(regExpression: "[A-Za-z0-9[]{}#%\\^*+=_\\|~<>€£¥•-/:;()$&@\\\".,?!\\']") == false { return false } But still getting the following crash: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_INVALID_RANGE (string a, pattern [A-Za-z0-9[]{}#%\^*+=_\|~<>€£¥•-/:;()$&@\".,?!\'], case 0, canon 0)' – Ashok Apr 17 '17 at 05:48