Solved

Help with regular expression for URL rewrite

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Running classic asp applications under Windows Server 2008 R2 (x64) and IIS 7 is not as easy as one may think. It took me a while to figure it out while getting error 8002801d a few times. After you install the OS you will need to install the fol…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

12 Experts available now in Live!

Get 1:1 Help Now