I checked this forum for a answer to my problem, but couldnt find. Experts please help.
I have a problem to validate a string say first name. I am given a set of rules w
Try this
[A-Za-z]+(?:[-'!` ]?[A-Za-z]+)?
You can check it online here: regexr
The second part
(?:[-'!` ]?[A-Za-z]+)?
is a non capturing group, this group is because of the ?
at the end optional.
Let Name be the regex for the simple name, Special the set of special characters (including the space). Then your final regex looks like
Name(SpecialName)?
What you need to do is repeat the text definition to have to exist 1 or more times AFTER the punctuation. This will work, but every name needs to be at least 2 characters (which seems reasonable..)
[A-Za-z]+[-'!` ]?[A-Za-z]+