title: The backslash character in Regex for Python
body: <p>In the <a href="https://docs.python.org/2/howto/regex.html" rel="nofollow noreferrer">Python documentation for Regex</a>, the author mentions:</p>
  
  <blockquote>
    <p>regular expressions use the backslash character ('\') to indicate
    special forms or to allow special characters to be used without
    invoking their special meaning. This conflicts with Python’s usage of
    the same character for the same purpose in string literals.</p>
  </blockquote>
  
  <p>He then goes on to give an example of matching <code>\section</code> in a regex:</p>
  
  <blockquote>
    <p>to match a literal backslash, one has to write '\\' as the RE
    string, because the regular expression must be \, and each backslash
    must be expressed as \ inside a regular Python string literal. In REs
    that feature backslashes repeatedly, this leads to lots of repeated
    backslashes and makes the resulting strings difficult to understand.</p>
  </blockquote>
  
  <p>He then says that the solution to this "backslash plague" is to begin a string with <code>r</code> to turn it into a raw string. </p>
  
  <p>Later though, he gives this example of using Regex:</p>
  
  <pre><code>p = re.compile('\d+')
  p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')
  </code></pre>
  
  <p>which results in:</p>
  
  <p><code>['12', '11', '10']</code></p>
  
  <p>I am confused as to why we did not need to include an <code>r</code> in this case before <code>'\d+'</code>. I thought, based on the previous explanations of backslash, that we'd need to tell Python that the backslash in this string is not the backslash that it knows. </p>