2

I'm trying to write a regexp that allow me to match a dynamic URL in Django 1.5

The URL will be like this: /file/namefile/ but namefile may contain one or more whitespaces that are converted in %20.

For example: URL: file/fourth test/ is saw as file/fourth%20test/.

The % character doesn't allow me to use something like this: ^file/(\w)/$ of course.

I need a regex that match that expression whatver the number of spaces (I don't really care about security in this stage of the project) but, being a total beginner, I'm stuck.

Higure
  • 235
  • 1
  • 5
  • 19
  • 1
    [Literal space character is not allowed in URLs.](http://stackoverflow.com/a/1547940/53114) Depending on the encoding, it may either be encoded as `%20` or as `+`. However, browsers may *display* those encoded spaces with an actual space. – Gumbo Dec 27 '13 at 17:17
  • I understood that, that's why I need a regexp that match also the `%20` – Higure Dec 27 '13 at 17:19
  • I don't know Django things. But will something like this work? ([\w\%\s\+]) – Sabuj Hassan Dec 27 '13 at 17:29
  • In your example, the incoming URI is something like `/file/fourth%20test/`? What do you want to do with it, and where (could affect whether you see it as `%20` or as a real space)? Is it in .htaccess or in scripting code such as PHP? – Phil Perry Dec 27 '13 at 17:40

1 Answers1

2

Try this:

^file/((?:\w|%20)+)/$

The ?: prevents the inner () from creating a reference.

svoop
  • 3,318
  • 1
  • 23
  • 41