Solved

Help with regular expression for URL rewrite

Posted on 2013-05-13
3
297 Views
Last Modified: 2013-05-22
Hello, I can't seem to get this regex working the way I want...

The pattern is pretty simple:

^page/([0-9]+)?/?([0-9]+)?/?

So the URI can be page/1/2/ or page/1/2 or page/1/ or page/1 (or just page/)

But the capture group is not right sometimes.

For page/1/2/ it works okay,

-The 1st capture group is 1
-The 2nd capture group is 2

But for page/1/
- The 1st capture group is nothing
- The 2nd capture group is 1

It should be the 1st capture group is 1 and the 2nd capture group is nothing!

What am I doing wrong?
0
Comment
Question by:intoxicated_curveball
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39163143
Perhaps try this:

^page/([0-9]*)/?([0-9]*)/?

It should have essentially the same meaning, but I'm hoping it will be more greedy so that the first capturing group captures what it should.
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 39164535
Maybe you should post your code of your use of the regex class.  Your expression seems to work for me in the tester I'm using.  I'm wondering if you're crossing the concept of groups and captures.

You could try a different expression.  The following creates a named group called "pagenum" that is expected to repeat 1 or 2 times.  Your match.Groups["pagenum"] reference would then have one or two captures in it.

^page(?:/(?<pagenum>[0-9]+)){1,2}/?$
0
 

Author Closing Comment

by:intoxicated_curveball
ID: 39188624
Thanks, seems to work!!!!!!!!!!!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
What is an ISAPI filter?   •      It's an assembly (.dll file) that can add or change the way IIS works.   •      They can be enabled globally for your web server or on a site-by-site basis.   When the IIS server receives a request, enabling the ISAPI fi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now