how to properly use a unicode string in python regex
<p>I am getting an input regular expression from a user which is saved as a unicode string. Do I have to turn the input string into a raw string before compliling it as a regex object? Or is it unnecessary? Am I converting it to raw string properly?</p>

<pre><code>import re
input_regex_as_unicode = u"^(.){1,36}$"
string_to_check = "342342dedsfs"

# leave as unicode
compiled_regex = re.compile(input_regex_as_unicode)
match_string = re.match(compiled_regex, string_to_check)

# convert to raw
compiled_regex = re.compile(r'' + input_regex_as_unicode)
match_string = re.match(compiled_regex, string_to_check)
</code></pre>

<p>@Ahsanul Haque, my question is more regular expression specific, whether the regex handles the unicode string properly when converting it into a regex object</p>

<p>The <code>re</code> module handles both unicode strings and normal strings properly, you do not need to convert them to anything (but you should be consistent in your use of strings).</p>

<p>There is no such a thing like "raw strings". You can use raw string notation in your code if it helps you with strings containing backslashes. For instance to match a newline character you could use <code>'\\n'</code>, <code>u'\\n'</code>, <code>r'\n'</code> or <code>ur'\n'</code>.</p>

<p>Your use of the raw string notation in your example does nothing since <code>r''</code> and <code>''</code> evaluate to the same string.</p>

python regex python-2.7