How to check a text contains \
<pre><code>def check(text):
    pattern = re.compile(r'\\')
    rv = re.match(pattern, text)
    if rv:
        return True
    else:
        return False

print check('\mi') # True
print check('\ni') # False
</code></pre>

<p>Actually,I want text contains '\' is illegal.</p>

<p>But '\n', '\b',etc, python treats them specially,so I can not match them out.</p>

<p>Any solutions?</p>

<pre><code>def check(text):
    rv = re.search('(\\\\)|(\\\n)', string)
    if rv:
        return True
    else:
        return False
string = "\mi"
print check(string) 
string = "\ni"
print check(string) 
</code></pre>

<p>Result:</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>================================ RESTART ===============</p>

<pre><code>True
True
</code></pre>
    </blockquote>
  </blockquote>
</blockquote>

<p><code>\\\n</code> includes newlines.
You can specifically search this way for <code>\n</code> by escaping <code>\\</code> and adding <code>\n</code>. Works with <code>\b</code> etc... </p>

python