Link to home
Start Free TrialLog in
Avatar of intoxicated_curveball
intoxicated_curveball

asked on

Help with regular expression for URL rewrite

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?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b_levitt
b_levitt

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}/?$
Avatar of intoxicated_curveball

ASKER

Thanks, seems to work!!!!!!!!!!!