Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex with single quote (') not validating single quote [closed]

Tags:

regex

swift

I'm using the following regex to validate the name:

> ^[a-zA-Z]{1}[a-zA-Z.' ]{1,20}$

The single quote is mentioned in the second range. However, when I validate a string against this regex, the single quote (') invalidates the match. Here's my code:

let nameRegEx = "^[a-zA-Z]{1}[a-zA-Z.' ]{1,21}$"
let nameTest = NSPredicate(format:"SELF MATCHES %@", nameRegEx)
let isNameValid = nameTest.evaluate(with: name)

I've tried \' but no use.

like image 989
HitMan Avatar asked Sep 02 '25 16:09

HitMan


1 Answers

Turns out textField.text returns and not '. Changing the character resolved the issue.

like image 137
HitMan Avatar answered Sep 05 '25 05:09

HitMan