Categories
Python Answers

How to use Python regular expression with Unicode?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *