What I am trying to do: Parse a query for a leading or trailing ? which will result in a search on the rest of the string.
“foobar?” or “?foobar” results in a search.
“foobar” results in some other behavior.
This code works as expected in the interpreter:
>>> import re
>>> print re.match(".+\?\s*$","foobar?")
<_sre.SRE_Match object at 0xb77c4d40>
>>> print re.match(".+\?\s*$","foobar")
None
This code from a Django app does not:
doSearch = { "text":"Search for: ", "url":"http://www.google.com/#&q=QUERY", "words":["^\?\s*",".+\?\s*$"] }
...
subQ = myCore.lookForPrefix(someQuery, doSearch["words"])
...
def lookForPrefix(query,listOfPrefixes):
for l in listOfPrefixes:
if re.match(l, query):
return re.sub(l,'', query)
return False
The Django code never matches the trailing “?”, all other regexs work fine.
And ideas about why not?
The problem is in your second regex. It matches the whole query, so using
re.sub()will replace it all with an empty string. I.e.lookForPrefix('foobar?',listOfPrefixes)will return''. You are likely checking the return value in anif, so it evaluates the empty string as false.To solve this, you just need to change the second regex to
\?\s*$and usere.search()instead ofre.match(), as the latter requires that your regex matches from the beginning of the string.The result:
EDIT: In fact, you might as well combine the two regexes into one:
^\?\s*|\?\s*$. That will work equally well.