a regular expression problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lookon

    a regular expression problem

    I want to use django to dispatch url.
    The url is like /test/Google/6,and my patten is r'^/test/(?P<q>\b\W+
    \b)/(?P<h>\d+)$'.
    It works when the string is English(like Google), but fails when the
    string is in foreign language.

    Can anyone tell me the righ regular expression?

    Thank you!
  • Bruno Desthuilliers

    #2
    Re: a regular expression problem

    lookon a écrit :
    I want to use django to dispatch url.
    The url is like /test/Google/6,and my patten is r'^/test/(?P<q>\b\W+
    \b)/(?P<h>\d+)$'.
    It works when the string is English(like Google), but fails when the
    string is in foreign language.
    Care to give an exemple of url that fails ?

    Anyway, if you want to match just *anything* in that path segment, you
    can try:

    r'^/test/(?P<q>.+?)/(?P<h>\d+)$'.



    Comment

    • Leefurong

      #3
      Re: a regular expression problem

      I want to use django to dispatch url.
      The url is like /test/Google/6,and my patten is r'^/test/(?P<q>\b\W+
      \W should be \w, a typo? :)
      \b)/(?P<h>\d+)$'.
      It works when the string is English(like Google), but fails when the
      string is in foreign language.
      Try this:
      r'(?u)^/test/(?P<q>\b\w+\b)/(?P<h>\d+)$'
      if ?u doesn't work, try ?L.
      I'm not sure which encoding a url uses.(locale or unicode? I guess
      unicode)
      >
      Care to give an exemple of url that fails ?
      >
      Anyway, if you want to match just *anything* in that path segment, you
      can try:
      >
      r'^/test/(?P<q>.+?)/(?P<h>\d+)$'.
      I don't think he wants to match anything :)

      Comment

      Working...