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?
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.
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
In perl you could do something like this.
perl -le '@m = ( "garbare=i123123123asdlfkjsaf&supplyId=123456789b&search=true" =~ /supplyId=(\d+\w+)&/g ); print for @m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With