Skip to content Skip to sidebar Skip to footer

Finding The Regex For ///?

This was posted to me <*@google.com> wrote: Hi Niklas, If you just want to map this: /region/city/category/ supposing its only this valid characters: [a-zA-Z0-9

Solution 1:

After you have given more details, I can now propose another regex pattern:

import re

reg = re.compile('(?:/[^/]+(?=/[^/]+/[^/]+/?\Z)'# this partial RE matches the# first of 3 groups, if 3'|'# OR')'# nothing is catched'/([^/]+)'# the group always catching something'(?:/([^/]+)?)?'# the possible second or third group'/?\Z' ) # the endfor ss in ('/frankfurt', '/frankfurt/',
           '/frankfurt/electronics', '/frankfurt/electronics/',
           '/eu/frankfurt/electronics', '/eu/frankfurt/electronics/',
           'toronto/lightnings', 'toronto/lightnings/',
           'lima/cars/old', 'lima/cars/old/',
           '/rio_de_janeiro/grande_rio_de_janeiro/casas/Magdalena'):
    mat = reg.match(ss)
    print ss,'\n',mat.groups() if mat else'- No matching -','\n'

result

/frankfurt 
('frankfurt', '') 
/frankfurt/ 
('frankfurt', '') 
/frankfurt/electronics 
('frankfurt', 'electronics') 
/eu/frankfurt/electronics/ 
('frankfurt', 'electronics') 
toronto/lightnings 
- No matching - 
lima/cars/old/ 
- No matching -
/rio_de_janeiro/grande_rio_de_janeiro/casas/Magdalena 
- No matching -

But, you know, using a regex isn't absolutely necessary to solve your problem:

for ss in ('/frankfurt', '/frankfurt/',
           '/frankfurt/electronics', '/frankfurt/electronics/',
           '/eu/frankfurt/electronics', '/eu/frankfurt/electronics/',
           'toronto/lightnings', 'toronto/lightnings/',
           'lima/cars/old', 'lima/cars/old/',
           '/rio_de_janeiro/grande_rio_de_janeiro/casas/Magdalena'):
    if ss[0]=='/':
        splitted = ss.rstrip('/').split('/')
        iflen(splitted)==2:
            grps = splitted[::-1]
        eliflen(splitted) in (3,4):
            grps = splitted[-2:]
        else:
            grps = Noneelse:
        grps = Noneprint ss,'\n',grps if grps else'- Incorrect string -','\n'

The results are the same as above.

Solution 2:

You can try

/(?:[^/]+)/?([^/]*)/?([^/]*)

which will put 'a/b' in variable 1, 'a' in variable 2 and 'b' in variable 3. Not sure if that is what you want.

Solution 3:

A solution that may work for you, though you may find it too hardcoded:

Routes for your app structured like this:

routes = [
    ('/foo/([a-zA-Z]+)/?', TestHandler),
    ('/foo/([a-zA-Z]+)/([a-zA-Z]+)/?', TestHandler),
    ('/foo/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/?', TestHandler)
]

And in your handler you'd check len(args), something like:

classTestHandler(webapp.RequestHandler):
    defget(self, *args):
        iflen(args): # assign defaults, perhaps?

Solution 4:

If you just want to map this: (region)/(city)/(category)/ supposing its only this valid characters: [a-zA-Z0-9_]

you can do the following: - main.py

application = webapp.WSGIApplication([
                    ('/([-\w]+)/([-\w]+)/([-\w]+)', Handler)
],debug=True)

and on your handler:

classHandler(webapp.RequestHandler):defget(self, region, city, category):
        # Use those variables on the method

Hope this helps!

Post a Comment for "Finding The Regex For ///?"