Which special characters must be escaped when using Python regex module re?
Asked Answered
S

1

7

I'm using the Python module re to write regular expressions for lexical analysis. I've been looking for a comprehensive list of which special characters must be escaped in order to be recognized by the regex to no avail. Can someone please point me to an exhaustive list?

The line in the current regex I'm writing that's giving me trouble is:

[\|\^&\+-%\*\/=!>]

I'd like it to recognize the characters: |^&+-%*/=!>

Have I not escaped something I should have?

Sigfrid answered 11/1, 2016 at 1:27 Comment(0)
W
10

After the opening square bracket the only special characters are -, ^ and ]:

[|\^&+\-%*/=!>]

You can find the list of special characters here:

Watercolor answered 11/1, 2016 at 1:30 Comment(6)
Thanks. That did it. Do you know of a list of special characters that must be escaped that I can reference?Sigfrid
@VictorBrunell okay, improved the answer and added some links. Hope that helps.Watercolor
That cheat sheet is exactly what I needed. Thanks.Sigfrid
You can add the backslash too.Bacteriolysis
@Casimir Do you mean the forward slash? The script runs fine when it's not escaped. Should I escape it for some reason?Sigfrid
@VictorBrunell: no I mean the literal backslash. When you want to figure a literal backslash inside or outside a character class, you need to escape it (with an other backslash). You don't need to escape the forward slash (except if you use an implementation where the slash is used as pattern delimiter, but it isn't the case in python).Bacteriolysis

© 2022 - 2024 — McMap. All rights reserved.