I'm trying to replace strings marked in both quotation mark styles (“...” and "...") on a string in Python.
I've already written a regex to replace the standard quotations
print re.sub(r'\"(.+?)\"', r'<em>"\1"</em>', self.title)
When I try to do it for the literary (?) ones it doesn't replace anything.
return re.sub(r'\“(.+?)\”', r'<em>“\1”</em>', self.title)
In fact, as I have it right now, I can't even make a conditional query:
quote_list = ['“', '”']
if all(character in self.title for character in quote_list):
print "It has literary quotes"
print re.sub(r'\“(.+?)\”', r'<em>“\1”</em>', self.title)
print re.sub(r'\"(.+?)\"', r'<em>"\1"</em>', self.title)
EDIT: Further context: It's an object
class Entry(models.Model):
title = models.CharField(max_length=200)
def render_title(self):
"""
This function wraps italics around quotation marks
"""
quote_list = ['“', '”']
if all(character in self.title for character in quote_list):
print "It has literary quotes"
return re.sub(r'\“(.+?)\”', r'<em>“\1”</em>', self.title)
return re.sub(r'\"(.+?)\"', r'<em>"\1"</em>', self.title)
I am not well-versed in regex commands. What am I doing wrong?
EDIT2: One step closer to the problem! It lies with the fact that I'm dealing with unicoded strings. I'm still stumped as how I can solve this. Any help is appreciated!
>>> title = u"sdsfgsdfgsdgfsdgs “ asd” asd"
>>> print re.sub(r'\“(.+?)\”', r'<em>“\1”</em>', title)
sdsfgsdfgsdgfsdgs “ asd” asd
>>> title = "sdsfgsdfgsdgfsdgs “ asd” asd"
>>> print re.sub(r'\“(.+?)\”', r'<em>“\1”</em>', title)
sdsfgsdfgsdgfsdgs <em>“ asd”</em> asd