2

I want to match only alphabetic characters, i.e a-z or A-Z, which can also contain spaces. The intent is to match any multiword names like 'Vivek Jha'. I expect the following Regex to work:

re.match(r'^[aA-zZ\s]+$', name)

It works for all the cases but also matches a word: 'Vivek_Jha'

I do not want and underscore to be matched. How is this _ getting matched.

I have worked on Regex in Perl and Tcl, but I think Python is doing something more that I can imagine.

3 Answers 3

6

A-z is capturing everything from ASCII character A to ASCII character z. This includes the _ character as well as many others. For more information on this, you can view Wikipedia's ASCII article.

To fix the problem, you need to do:

re.match(r'[a-zA-Z\s]+$', name)

This tells Python to only capture characters in the ASCII ranges a-z and A-Z.

Also, I removed the ^ because re.match matches from the start of the string by default.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to have helped! Please do not forget to accept an answer by clicking the check next to it. Doing so lets people know that this problem is solved.
4

If you want to match only alphabetic characters,which can also contain spaces just use :

r'^[a-zA-Z ]+$'

note that aA-zZ is wrong way for match letters you must use a-z for lowercase and A-Z for upper case . Note :

The \s metacharacter is used to find a whitespace character.

A whitespace character can be:

A space character
A tab character
A carriage return character
A new line character
A vertical tab character
A form feed character

Comments

2

Try a-zA-Z instead of aA-zZ.

a-z have nothing between them but letters, same for A-Z, but A-z have a lot of stuff in between... apparently including the underscore character.

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.