Adam
Adam

Reputation: 3128

Check if string ends with something using regex

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

Answers (2)

user2555451
user2555451

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:

  1. The pattern needed to have *. changed to .* (this is what caused the error).
  2. You need to use re.search here, which searches for a pattern in a string, instead of re.match, which looks for an exact match.
  3. You don't need the /'s at the start and end of Python Regexes.
  4. / does not need to be escaped in </div>

Upvotes: 3

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

Change you regex to:

r'<\/div> <img src="[.]{3}" title"(.*)">$'
  1. . has a special meaning in regex, so you should use a character class or escape it using \.

  2. No need to enclose regex between / / in Python.

  3. *. should be .*.

  4. 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

Related Questions