-1

I am currently learning regular expression in Python, and I am confused by what regular expression really matches. For example, '\\' is actually '\', so what if I need to match the '\' in string 'abc\\cde'? re.findall(r'\', '010\\aa') doesn't work at all. Thanks for any help.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Jieyu You
  • 37
  • 7

2 Answers2

0

As others have mentioned, \ is one character you still need to escape in raw strings:

re.findall(r'\\', '010\\aa')

Note that r'\\' is a string of length two, containing two backslashes, not one, unlike '\\'. This is described at the bottom of https://docs.python.org/2.0/ref/strings.html .

aaronm04
  • 68
  • 1
  • 5
  • `len(r'\x07')` is 4 characters, so it is not equivalent to `'\a'`. I think the regular expression library is doing its own conversion so that this can match `'\a'`. – aaronm04 Jul 14 '17 at 22:37
  • And Python refuses to remove escapes when it parses raw literals, which is dumb. –  Jul 15 '17 at 02:43
0

You always need to escape the delimiter in strings.
Since languages need to parse an escaped delimiter,
this requires parsing the escape itself.

This is a fact of language source string parsing, even raw strings.

Examples:

' => r'\''
\' => r'\''
\\' => r'\\\''