Solved

Help with regular expression for URL rewrite

Posted on 2013-05-13
3
323 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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