user393273
user393273

Reputation: 1438

Path Regular Expression - Allow only one level

I would like a regular expression that matches the following:

/home
/news
/blog
/home/
/news/
/blog/

However, it shouldn't match:

/product/logs/barbeque
/product/1
/blog/post/1

/home and such are not fixed - it should match that structure of the URL

Upvotes: 0

Views: 1763

Answers (2)

jensgram
jensgram

Reputation: 31508

As the question is presented, this ought to do:

#^/[^/]+$#

This will match anything starting with a forward slash (/) and then at least one character (which cannot be a /), e.g., /home, /news, and /blog.

/product/logs/barbeque, /product/1, and /blog/post/1 will all fail on the "cannot be a /" part.


UPDATE
As for the updated matches /home/, /news/, and /blog/, consider this:

#^/[^/]+/?$#

Upvotes: 3

invarbrass
invarbrass

Reputation: 2103

Not quite sure what you're trying to do.

If you're searching for specific words in the URL, why not use string comparison functions directly? For the second set of URLs (which you don't want to match), regex is preferable.

However, in your case you'll be better off using strcmp() and friends directly.

Upvotes: -1

Related Questions