7

I get a query string from url by saying request.queryString() as -

supplyId=123456789b&search=true

I want to replace the value for "supplyId" with a new value. "supplyId" can come at any position in the query string. What would be the possible regular expression for this?

1
  • 1
    Why use a regular expression when you know what your delimiters look like? Commented May 17, 2012 at 19:33

5 Answers 5

7

I wouldn't actually use regex for this, but string manipulation. Search for the position of "supplyId=" in the URL, then grab everything until the end of the string or "&", whichever comes first.

If you have to use a regex, try one of these:

(?<=supplyId=)[^&]+

supplyId=([^&]+)

Make sure case sensitivity is off. If you use the second pattern, the value you want will be in capture group 1.

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

4 Comments

what happens if value is: abcdsupplyId=value
@djmj - That shouldn't be a problem, since the string on the left of the = is the parameter name. You could just give your other parameters names that don't end in supplyid. But just for fun, you can solve that by adding \b to the beginning: (?<=\bsupplyId=)[^&]+ or \bsupplyId=([^&]+)
Isn't \b for empty word? The delimiter & or query separator ? should be detected at front unless they are escaped.
@djmj - \b means a word boundary. It will prevent any letter, number, or underscore before supplyId, so & and ? will both trigger it. BTW, you can't escape those in a URL, unless you were talking about hexadecimal URL encoding. Any hex-encoded character would end in [0-9A-F], all of which would be blocked by \b as it should be. But as I said, it's basically moot. You're only matching the parameter name, which you yourself control.
5

I think you should be able to do something like so:

String queryString = "supplyId=123456789b&search=true";
String anyStringIlike = "someValueIlike";
String newQueryString = queryString.replaceAll("supplyId=[^&]+","supplyId=" + anyStringIlike);
System.out.println(queryString);
System.out.println(newQueryString);

This should print:

supplyId=123456789b&search=true

supplyId=someValueIlike&search=true

1 Comment

what happens if value is: abcdsupplyId=value ?
2

In perl you could do something like this.

perl -le '@m = ( "garbare=i123123123asdlfkjsaf&supplyId=123456789b&search=true" =~ /supplyId=(\d+\w+)&/g ); print for @m

Comments

1
public static String updateQueryString (String queryString, String name, String value) {
if (queryString != null) {
      queryString = queryString.replaceAll(name + "=.*?($|&)", "").replaceFirst("&$", "");
   }
 return addParameter(queryString, name, value);
}

public static String addParameter(queryString, name, value) {      
  return StringUtils.isEmpty(queryString) ? (name + "=" + value) : (queryString + "&" + name + "=" + value);

}

You call like: updateQueryString("supplyId=123456789b&search=true", "supplyId", "newValue");

output: search=true&supplyId=newValue

Comments

1
public class TestQueryStringValReplace {    
   public static String replace(String queryString, String propName, String newVal) {
       return queryString.replaceAll(propName+"=[^&]+", propName+"=" + newVal);
   }

   public static void main(String[] args) {
       Assert.assertEquals("supplyId=newVal&search=true", replace("supplyId=oldVal&search=true", "supplyId","newVal"));
       Assert.assertEquals("supplyId=newVal", replace("supplyId=oldVal", "supplyId","newVal"));
       Assert.assertEquals("search=true&supplyId=newVal&3rdprop=val", replace("search=true&supplyId=oldVal&3rdprop=val", "supplyId","newVal"));
   }

}

Comments

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.