Fixer
Fixer

Reputation: 6095

How to match a pattern but exclude from the result

Using the IIS URL Rewrite Module I am trying rewrite an incoming Url from /en/page.aspx to /en/page/index.aspx

Using regular expression I am checking that the string ends with .aspx

^.*(.aspx)$

Currently in capture group I am getting

/en/page.aspx

How can i only capture /en/page part?

Further down the line I will rewrite the Url as follows:

{R:0}/index.aspx

Upvotes: 0

Views: 718

Answers (2)

Efran Cobisi
Efran Cobisi

Reputation: 6454

To match without capturing you can use the ?: pattern, like (?:\.aspx). However, to suit your redirection needs you may want to use the following expression:

^(.*)(\.aspx)$

So that {R:1} will point to exactly what you need. Also note you need to escape the dot, so that the expression will match the dot character and not every other one.

Upvotes: 2

gislikonrad
gislikonrad

Reputation: 3581

Change how you capture matches...

^(.*?)(\.aspx)$

When going through the matches collection, the matches should be the following:

index 0 should be the whole url

index 1 should be /en/page

index 2 should be .aspx

Upvotes: 1

Related Questions