Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiple repeat Error

Tags:

python

regex

I'm trying to determine whether a term appears in a string.
Before and after the term must appear a space, and a standard suffix is also allowed.
Example:

term: google string: "I love google!!! " result: found  term: dog string: "I love dogs " result: found 

I'm trying the following code:

regexPart1 = "\s" regexPart2 = "(?:s|'s|!+|,|.|;|:|\(|\)|\"|\?+)?\s"   p = re.compile(regexPart1 + term + regexPart2 , re.IGNORECASE) 

and get the error:

raise error("multiple repeat") sre_constants.error: multiple repeat 

Update
Real code that fails:

term = 'lg incite" OR author:"http++www.dealitem.com" OR "for sale' regexPart1 = r"\s" regexPart2 = r"(?:s|'s|!+|,|.|;|:|\(|\)|\"|\?+)?\s"  p = re.compile(regexPart1 + term + regexPart2 , re.IGNORECASE) 

On the other hand, the following term passes smoothly (+ instead of ++)

term = 'lg incite" OR author:"http+www.dealitem.com" OR "for sale' 
like image 285
Presen Avatar asked Nov 12 '13 23:11

Presen


People also ask

How do you escape special characters in regex python?

Escaping with \ To match the metacharacters literally, i.e. to remove their special meaning, prefix those characters with a \ (backslash) character. To indicate a literal \ character, use \\ .

What is re escape?

You can use re. escape() : re. escape(string) Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

What is re compile in Python?

Python's re. compile() method is used to compile a regular expression pattern provided as a string into a regex pattern object ( re. Pattern ). Later we can use this pattern object to search for a match inside different target strings using regex methods such as a re. match() or re.search() .


2 Answers

The problem is that, in a non-raw string, \" is ".

You get lucky with all of your other unescaped backslashes—\s is the same as \\s, not s; \( is the same as \\(, not (, and so on. But you should never rely on getting lucky, or assuming that you know the whole list of Python escape sequences by heart.

Either print out your string and escape the backslashes that get lost (bad), escape all of your backslashes (OK), or just use raw strings in the first place (best).


That being said, your regexp as posted won't match some expressions that it should, but it will never raise that "multiple repeat" error. Clearly, your actual code is different from the code you've shown us, and it's impossible to debug code we can't see.


Now that you've shown a real reproducible test case, that's a separate problem.

You're searching for terms that may have special regexp characters in them, like this:

term = 'lg incite" OR author:"http++www.dealitem.com" OR "for sale' 

That p++ in the middle of a regexp means "1 or more of 1 or more of the letter p" (in the others, the same as "1 or more of the letter p") in some regexp languages, "always fail" in others, and "raise an exception" in others. Python's re falls into the last group. In fact, you can test this in isolation:

>>> re.compile('p++') error: multiple repeat 

If you want to put random strings into a regexp, you need to call re.escape on them.


One more problem (thanks to Ωmega):

. in a regexp means "any character". So, ,|.|;|:" (I've just extracted a short fragment of your longer alternation chain) means "a comma, or any character, or a semicolon, or a colon"… which is the same as "any character". You probably wanted to escape the ..


Putting all three fixes together:

term = 'lg incite" OR author:"http++www.dealitem.com" OR "for sale' regexPart1 = r"\s" regexPart2 = r"(?:s|'s|!+|,|\.|;|:|\(|\)|\"|\?+)?\s"   p = re.compile(regexPart1 + re.escape(term) + regexPart2 , re.IGNORECASE) 

As Ωmega also pointed out in a comment, you don't need to use a chain of alternations if they're all one character long; a character class will do just as well, more concisely and more readably.

And I'm sure there are other ways this could be improved.

like image 72
abarnert Avatar answered Sep 28 '22 15:09

abarnert


The other answer is great, but I would like to point out that using regular expressions to find strings in other strings is not the best way to go about it. In python simply write:

    if term in string:          #do whatever 
like image 39
Patrick Avatar answered Sep 28 '22 17:09

Patrick