Reputation: 3128
I want to check if a haystack ends with a needle containing some text that changes.
Here's what I tried and is showing "sre_constants.error: nothing to repeat" error:
import re
string = 'stuff ... </div> <img src="..." title"SOME_TEXT_THAT_CHANGES">'
pattern = r'/<\/div> <img src="..." title"(*.)">$/'
prog = re.compile(pattern)
result = prog.match(string)
print result
Upvotes: 1
Views: 9687
Reputation:
This is how it should be:
>>> import re
>>> string = 'stuff ... </div> <img src="..." title"SOME_TEXT_THAT_CHANGES">'
>>> pattern = r'</div> <img src="..." title"(.*)">$'
>>> prog = re.compile(pattern)
>>> result = prog.search(string)
>>> result
<_sre.SRE_Match object at 0x0188A3A0>
>>> print result.group(1)
SOME_TEXT_THAT_CHANGES
>>>
Now let me go over the changes I made:
*.
changed to .*
(this is what caused the error).re.search
here, which searches for a pattern in a string, instead of re.match
, which looks for an exact match./
's at the start and end of Python Regexes./
does not need to be escaped in </div>
Upvotes: 3
Reputation: 250961
Change you regex to:
r'<\/div> <img src="[.]{3}" title"(.*)">$'
.
has a special meaning in regex, so you should use a character class or escape it using \
.
No need to enclose regex between / /
in Python.
*.
should be .*
.
re.match
matches only at the start of the string, so better use re.search
.( re.search()
vs re.match()
)
Demo:
>>> pattern = r'<\/div> <img src="[.]{3}" title"(.*)">$'
>>> prog = re.compile(pattern)
>>> prog.search(string)
<_sre.SRE_Match object at 0xb5dcf920>
Upvotes: 2