5

I am trying to test whether a character is a special character or not.

It fails for '-' character when I write the following code:

import re

s = '-'
regex = re.compile('[!@#$%^&*()-+]')

if regex.search(s) == None:
    print("Not found")
else:
    print("Found")

Output>>Not found

However, if I change the position of the '-' character in the pattern as follows (line 3 of code), it works correctly

import re 

s = '-'
regex = re.compile('[!@#$%^&*()+-]')

if regex.search(s) == None:
    print("Not found")
else:
    print("Found")

Output>>Found

What is causing this difference and how can I make sure that the characters will be detected?

mangom
  • 467
  • 1
  • 3
  • 20
  • 2
    I'll give you a hint: look at the characters in the character class `[a-z]`. It's more than three characters long! – Adam Smith Dec 10 '18 at 05:41
  • 1
    Try escaping characters like re.compile('[\-]') – shirish Dec 10 '18 at 05:44
  • Got it. It should be either the first or the last character in the range. Also works with an escape character: regex = re.compile('[!@#$%^&*()\-+]'). Thanks! – mangom Dec 10 '18 at 05:44
  • Hi, please take a look at the two questions I've marked. This is a common issue that has been asked before. If they don't address your problem please add more details to your question. – revo Dec 10 '18 at 07:14

2 Answers2

5

- is treated as a special character if it is not the last or the first character in a range and not escaped. So:

  • [-19] or [19-] or [1\-9] is -, 1 or 9, but
  • [1-9] is anything between 1 and 9, inclusive, but not - itself.
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    From [the docs](https://docs.python.org/3/library/re.html?highlight=re#regular-expression-syntax): "Ranges of characters can be indicated by giving two characters and separating them by a `'-'`, for example `[a-z]` will match any lowercase ASCII letter, `[0-5][0-9]` will match all the two-digits numbers from `00` to `59`, and `[0-9A-Fa-f]` will match any hexadecimal digit. If `-` is escaped (e.g. `[a\-z]`) or if it’s placed as the first or last character (e.g. `[-a]` or `[a-]`), it will match a literal `'-'`." – Adam Smith Dec 10 '18 at 05:43
0

'-' in Regex means between. So the first expression says

regex = re.compile('[!@#$%^&*()-+]')

Match characters between ')' and '+'

https://docs.python.org/2/library/re.html

t3pleni9
  • 11
  • 3