-1

I was trying to filter some data using regular expressions, and came with one example that doesn't compile in python, even though it seems to be a valid expression.

I have reduced it to its minimal form :

test=re.compile(r'[e-+]')

Calling this in Python 3.6.1, I get an error. I don't get an error with r'[e+-]', but I do get an error with r'[e-+]' ...

I don't understand why I get an error, because I know that '+' is a metacharacter, but in the manual : 'Metacharacters are not active inside classes'.

And I don't see why the order of the '-' and the '+' should matter ...

user5664615
  • 400
  • 1
  • 10
  • 17
Piea
  • 9
  • 4
  • possible dupe: https://stackoverflow.com/questions/3697202/including-a-hyphen-in-a-regex-character-bracket it's the hypen that is blowing you up. – sniperd Jul 31 '17 at 15:22
  • `"Valid regular expresssion doesn't compile in Python"` It's not a valid regex: https://regex101.com/r/FuVH24/1 – DeepSpace Jul 31 '17 at 15:24

2 Answers2

2

In a regular expression, the construct [a-e] matches 'a', 'b', 'c', 'd', or 'e'. To explicitly match a '-', you must either escape it \- or put it right after the opening bracket or right before the closing bracket.

Ruud de Jong
  • 744
  • 3
  • 11
1

The character '+' comes before the letter 'e', so the range '+-e' would make sense and matches any of the 59 characters from '+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcde'`, but 'e-+' is not allowed because the start of the range comes after the end.

The character set e+- is completely different. That one just has the three characters mentioned as the - does not form a range when it is the first or last character.

Duncan
  • 92,073
  • 11
  • 122
  • 156