user2498079
user2498079

Reputation: 3012

Regular Expression returns nothing

I have written the following regular expression to return everything except alphabets & letters. However this regular expression returns nothing. What can be the regular expression for such case?

Regex:

r'[^[a-z]+]'

Regards

Upvotes: 2

Views: 80

Answers (2)

user557597
user557597

Reputation:

This is how the regex engine see's your regex

 [^[a-z]+          # Not any of these characters  '[', nor a-z
 ]                 # literal ']'

So, as @Sajuj says, just need to remove the outer square brackets [^a-z]+

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

You are messing with the character class []. Here is the correct one(without uppercase):

r'[^a-z]+'

If you want to match with start and end of string, including Upper case letters.

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

And here is how you can use it:

print re.findall(r'([^a-zA-Z]+)', input_string)

() means capture the group so that it returns after the matching is performed.

Upvotes: 2

Related Questions