1

I have the next regular expression to find emojis on a text:

re.compile(u'([\U00002600-\U000027BF])|([\U0001F300-\U0001F64F])|([\U0001F680-\U0001F6FF])')

It is working well in Python 3 but in Python 2.7 I get this:

sre_constants.error: bad character range

How can I fix it to support both, Python 2.7 and Python 3?

1 Answer 1

1

Use r'(... instead of u'(... like this:

re.compile(r'([\U00002600-\U000027BF\U0001F300-\U0001F64F\U0001F680-\U0001F6FF])')

Also note that you can specify multiple ranges inside [...]

https://regex101.com/r/WuQ3Zr/1

Sign up to request clarification or add additional context in comments.

4 Comments

It's interesting. I tried your solution in interactive mode and it works but when I execute the program or when I run pytest, it fails. What could be the cause?
Which version of Python failed?
The interpreter is Python 2.7.0 and that code inside a file run by python <file> works ok. It's only in my system when running the cli or running pytest locally. This is the code github.com/davidmogar/cucco/tree/executable and this is the build in Travis travis-ci.org/davidmogar/cucco/builds/238482411 Do you think that is only a problem with my system or this could happen to others? I'm running it on a Macbook.
Just tried again in a virtual environment with 3.6.0 and still fails in local. The build for 3.6.0 in Travis works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.