regex + Python: How to find string with '?' in it? -
i have multi-line string in content
variable, , need retreive matches pattern uri
containing question mark in it.
this have far:
content = """ /blog:text:lorem ipsum dolor sit amet, consectetur adipisicing elit <break> text:duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore <break> text:excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia. /blog?page=1:text:lorem ipsum dolor sit amet, consectetur adipisicing elit <break> text:duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore <break> text:excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia. """ #uri = '/blog' # works fine uri = '/blog?page=1' re.findall('^(?ism)%s?:(.*?)(\n\n)' % uri, content)
it works fine until uri
gets ?
parameters after it, , empty list.
any ideas how fix regex?
python's re.escape()
friend. if don't use it, ?
inside uri treated usual meaning inside of regular expression (making prior item 0-or-1 match).
uri = '/blog?page=1' re.findall('^(?ism)%s?:(.*?)(\n\n)' % re.escape(uri), content)
i'm not clear want ?:
after the %s
do, i'm leaving in on potentially-faulty presumption it's there reason.
Comments
Post a Comment