emanuele
emanuele

Reputation: 2589

How to fix "nothing to repeat" regex error?

I know from this question that, nothing to repeat in a regex expression, is a known bug of python. But I must compile this unicode expression

re.compile(u'\U0000002A \U000020E3')

as a unique character. This is an emoticon and is a unique character. Python understand this string as u'* \\u20e3' and rise me 'nothing to repeat' error. I am looking around but I can't find any solution. Does exist any work around?

Upvotes: 0

Views: 1897

Answers (2)

Yoav Glazner
Yoav Glazner

Reputation: 8066

You need to use re.escape (as shown in "Thomas Orozco" answer) But use it only on the part that is dynamic such as:

print re.findall( u"cool\s*%s" % re.escape(u'\U0000002A \U000020E3'),
               u"cool      * \U000020E3 crazy")

Upvotes: 0

Thomas Orozco
Thomas Orozco

Reputation: 55293

This has little to do with the question you linked. You're not running into a bug. Your regex simply has a special character (a *) that you haven't escaped.

Simply escape the string before compiling it into a regex:

re.compile(re.escape(u'\U0000002A \U000020E3'))

Now, I'm a little unsure as to why you're representing * as \U0000002A — perhaps you could clarify what your intent is here?

Upvotes: 6

Related Questions