How to check a string matched with multiple patterns?

python, python-2.7, regex, tornado

Solution

If you want to match the entire URL against your pattern, you can use '^' and '$' to match the beginning and the end of the string to match.

In your example you could use

f = re.compile('|'.join( '(^'+p+'$)' for p in pat ))

to get the regular expression

'(^/FoodListAdminCP/Login[/]?$)|(^/FoodListAdminCP[/]?$)'

from your `pat` list.

Problem

I want to check an URL in definition pattern list. My pattern list is: ``` pat = ['/FoodListAdminCP/Login[/]?', '/FoodListAdminCP[/]?'] ``` I used this code for check the URL matched with one item of this list ``` import re f = re.compile('|'.join(pat)) if f.match(self.request.uri): self.login = True else: self.login = False ``` Now, if I request `/FoodListAdminCP/Dashboard` as URL, that matched with pattern. Because start of this URL matched with `'/FoodListAdminCP[/]?'` who is in my list. I want my request URL matched with entire of list item not part of that. How I can do it?

Original source