0

I am working on a Swift project and I need to use this regex to check email is valid or not but when the app start the checking the app crash and give me this error:

NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_MISSING_CLOSE_BRACKET

This is my REGEX:

^(([^<>()[\\]\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+[\\.]*)+[a-zA-Z]{2,}))$
1
  • 1
    The point is that you need to escape [ and ] inside a character class, use "^(([^<>()\\[\\],;:\\s@\"]+(\\.[^<>()\\[\\],;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z0-9-]+[.]*)+[a-zA-Z]{2,}))$" Commented Sep 6, 2016 at 12:10

1 Answer 1

1

Check unescaped brackets in your regex pattern:

let pattern
= "^(([^<>()[\\]\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))"
//    [     [                 ]     [     [                 ]
+ "@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+[\\.]*)+[a-zA-Z]{2,}))$"
//       [   ]        [   ]        [   ]        [   ]            [            ] [   ]   [      ]

You have some mismatching brackets [ ] in the first half of your pattern.

In some dialects of regex, you have no need to escape [ between [ and ], but in some other dialects, you need it.

Try adding some escapes to your regex:

let pattern
= "^(([^<>()\\[\\]\\.,;:\\s@\\\"]+(\\.[^<>()\\[\\]\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))"
//    [     ^^                  ]     [     ^^                  ]
+ "@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+[\\.]*)+[a-zA-Z]{2,}))$"
//       [   ]        [   ]        [   ]        [   ]            [            ] [   ]   [      ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.