Solved

Help with regular expression for URL rewrite

Posted on 2013-05-13
3
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Prologue It is often required to host multiple websites on a single instance of IIS, mostly in development environments instead of on production servers. I am sure it is not much a preferred solution on production servers but this is at least a pos…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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