Regular Expression to match "\\r"
<p>I'm having trouble writing a regex that matches these inputs: <br>
1.<code>\\r</code><br>
2.<code>\\rSomeString</code>
<br>
I need a regex that matches <code>\\r</code></p>

<p>A literal backslash in Python can be matched with <code>r'\\'</code> (note the use of the raw string literal!). You have two literal backslashes, thus, you need 4 backslashes (in a raw string literal) before <code>r</code>. </p>

<p>Since you may have any characters after <code>\\r</code>, you may use</p>

<pre><code>import re
p = re.compile(r'\\\\r\S*')
test_str = r"\\r \\rtest"
print(p.findall(test_str))
</code></pre>

<p>See <a href="http://ideone.com/kUK56Q" rel="nofollow">Python demo</a></p>

<p><strong>Pattern description</strong>:</p>

<ul>
<li><code>\\\\</code> - 2 backslashes</li>
<li><code>r</code>  - a literal <code>r</code></li>
<li><code>\S*</code> - zero or more non-whitespace characters.</li>
</ul>

<p><em>Variations</em>:</p>

<ul>
<li>If the characters after <code>r</code> can only be alphanumerics or underscore, use <code>\w*</code> instead of <code>\S*</code></li>
<li>If you want to only match <code>\\r</code> before non-word chars, add a <code>\B</code> non-word boundary before the backslashes in the pattern.</li>
</ul>

python regex