Salman
Salman

Reputation: 463

Regex for extenionless URL

In my asp.net application i have url www.site.com/rugby to match it i have regex

"~/(.+)" which works perfectly fine. But if i navigate to www.site.com/login.aspx again regex matches with this expression "/(.+)".

In Simple words i want a regex which only match the extenionless url. IF extension is present in the url then do not match it thanks

<RewriterRule>
                <LookFor>~/(.+)/(.+)/(.+)/</LookFor>
                <SendTo>~/Shop/Item.aspx?cn=&1&amp;it=$2&amp;ft=$3</SendTo>
</RewriterRule>

Upvotes: 1

Views: 209

Answers (3)

Falle1234
Falle1234

Reputation: 5063

you could try with this regex.

/([^?\./]*)(?![^?\./]*[?\.])

It should only match a path component and not a file.

So for these Url's:

/Shop/Item.aspx?cn=&1&it=$2&ft=$3 
/Shop/Item?cn=&1&it=$2&ft=$3 
/Shop/Item.aspx
/Shop/Item

The three first would return a single match, namely "Shop", but the last onw would return both "Shop" and "Item"

Upvotes: 0

James Curran
James Curran

Reputation: 103535

/([^?\.]*)(\?.*)?$ works correctly for each of these:

/Shop/Item.aspx?cn=&1&it=$2&ft=$3 
/Shop/Item?cn=&1&it=$2&ft=$3 
/Shop/Item.aspx
/Shop/Item

The translation of that Regex (as provided by the "Regular Expression Workbench")

/
Capture
  Any character not in "?\."
  * (zero or more times)
End Capture
Capture
  ?
  . (any character)
  * (zero or more times)
End Capture
? (zero or one time)
$ (anchor to end of string)

Upvotes: 0

ase
ase

Reputation: 13481

Actually .Net seems to have a built in URI parser that you should use instead. There is absolutely no reason the reinvent the wheel using regular expressions.

Check this site out for an example: http://cf-bill.blogspot.com/2008/07/c-parsing-url-for-its-component-parts.html also the documentation here: http://msdn.microsoft.com/en-us/library/system.uri.aspx

Edit: I re-read your question and this might not be what you are looking for, I would advise you to try to rewrite your question more clearly.

Upvotes: 1

Related Questions