0

string - URL:

1. &PARAM1=dummy&RQNB=EPR-0-1&PG=REQ //need to extract RQNB=EPR-0-1
2. &PARAM1=dummy&PG=REQ&RQNB=EPR-0-2 //need to extract RQNB=EPR-0-2
3. &RQNB=EPR-0-3&PARAM1=dummy&PG=REQ //need to extract RQNB=EPR-0-3

Need to extract RQNB=dyn_value. Will use in mod_rewrite for apache.

"RQNB=dyn_value" can appear in any position of the string

Tried:

^.*[&]?RQNB=(.*)$

but that extracts:

1) RQNB=EPR-0-1&PG=REQ
2) RQNB=EPR-0-2
3) RQNB=EPR-0-3&PARAM1=dummy&PG=REQ
2
  • 2
    What work have you done to attempt to solve this yourself? And more importantly, why aren't you using a library that already knows how to parse URLs? Commented Jun 3, 2014 at 17:38
  • posted what I tried. I will use in mod_rewrite for apache Commented Jun 3, 2014 at 17:41

1 Answer 1

2

This regex will do it:

(^|\b)RQNB=[^&]+

Regular expression visualization

Debuggex Demo

Edit:

Based on your comments below, you need to capture the string by enclosing it in ( and ), like so:

(?:^|\b)(RQNB=[^&]+)

Regular expression visualization

Debuggex Demo

Then, you can refer to RQNB=foo using a number group indicator, like $1.

Sign up to request clarification or add additional context in comments.

14 Comments

Will use in mod_rewrite for apache.
To use it in a RewriteCond, you are going to want to capture the information somehow using a group (with parentheses). Do you want to capture the entire string RQNB=foo, just foo, or RQNB and foo separately?
You need a combined RewriteCond, not two conditions.
Be sure you're using the one currently in my comment; it had a typo (missing ?:) that I fixed a moment after I posted it.
You're right. You need to do one of two things: (1) use two rules, one for each order of the arguments, or (2) use a combined rule that tests both possibilities. A demo of the latter is here. If you go that route, you may have data in four places (%1, %2, %3, %4), so your rule would have to look something like RewriteRule ^index\.php$ /epr/active-requests?%1%2%3%4 [NC,R=301]. You're better off doing two rules to keep this from balooning in complexity, if you can't control the order of the parameters. Ultimately, this logic may need to be in your app itself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.