0

I understand the concept of backslashes and their use in escape sequences, but

Why do these produce the same results?

import re

print 'Test 1'

for i in re.findall("\n", "This\nis\na\ntest."):
    print 'Newline'

print 'Test 2'

for i in re.findall(r"\n", "This\nis\na\ntest."):
    print 'Newline'

Try the code here in an online Python compiler.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • 1
    Maybe this will be helpful: [link](http://stackoverflow.com/questions/12871066/what-exactly-is-a-raw-string-regex-and-how-can-you-use-it) – Marcin Feb 25 '15 at 00:32

1 Answers1

3

In the first case, you are asking the regex engine to search for a newline character. Python converts the \n to a newline character and passes it to re.findall.

In tthe second case, you are asking the regex engine to search for \n. Python passes \n to re.findall. The regex engine knows that \n means newline.

So you get the same result; the unescaping of the \n merely happens at a different stage of the process.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • So the regex interprets the pattern as it pleases? Even if you explicitly supply a different pattern. – Malik Brahimi Feb 25 '15 at 00:37
  • It doesn't interpret it "as it pleases," it interprets it as it is documented to do. You have supplied two different patterns, but they match the same character, so you get the same result. – kindall Feb 25 '15 at 00:37
  • So how to you distinguish if you want to search for a literal backslash with an n? – Malik Brahimi Feb 25 '15 at 00:42
  • Escape the backslash, e.g. `r"\\n"` – kindall Feb 25 '15 at 00:44
  • Doesn't that defeat the purpose of the raw string modifier? – Malik Brahimi Feb 25 '15 at 00:45
  • No. Without the raw string modifier you'd need to write `"\\\\n"`. – kindall Feb 25 '15 at 00:46
  • To elaborate further, you have to get the backslash through *two* layers of unescaping. Python unescapes the string (unless you have `r` in front of it), and then the regex engine *also* does it. So it takes four backslashes in a normal Python string literal to search for one backslash using a regex. – kindall Feb 25 '15 at 00:52