Menu Close

How to use Python regular expression with Unicode?

Sometimes, we want to use Python regular expression with Unicode.

In this article, we’ll look at how to use Python regular expression with Unicode.

How to use Python regular expression with Unicode?

To use Python regular expression with Unicode, we can use the re.compile method.

For instance, we write

myre = re.compile(ur'[\u0000\u0000\u0000\u0000\u0000\u0000]+', 
                      re.UNICODE)

to call re.compile with the Unicode regex string ur'[\u0000\u0000\u0000\u0000\u0000\u0000]+'.

The 2nd argument is re.UNICODE so re.compile will treat the first argument as a Unicode regex string.

Conclusion

To use Python regular expression with Unicode, we can use the re.compile method.

Posted in Python, Python Answers