From: Jimmy on
Below are the example of possible input strings:

myparam1=myvalue1&param1=value2&param3=value3
&myparam1=myvalue1&param1=value2&param3=value3
?myparam1=myvalue1&param1=value2&param3=value3
"myparam1=myvalue1&param1=value2&param3=value3"
"&myparam1=myvalue1&param1=value2&param3=value3"
"?myparam1=myvalue1&param1=value2&param3=value3"

I like to replace value of "param1" with "somevalue". Can it be done
in 1 expression replacement? Cuz pattern [\"&?]* works for searching,
but reusing the same pattern will get rid of the first non-alpha
character.
From: Lew on
On 05/19/2010 05:01 PM, Jimmy wrote:
> Below are the example of possible input strings:
>
> myparam1=myvalue1&param1=value2&param3=value3
> &myparam1=myvalue1&param1=value2&param3=value3
> ?myparam1=myvalue1&param1=value2&param3=value3
> "myparam1=myvalue1&param1=value2&param3=value3"
> "&myparam1=myvalue1&param1=value2&param3=value3"
> "?myparam1=myvalue1&param1=value2&param3=value3"
>
> I like to replace value of "param1" with "somevalue". Can it be done
> in 1 expression replacement? Cuz pattern [\"&?]* works for searching,
> but reusing the same pattern will get rid of the first non-alpha
> character.

String.replaceAll( "param1", "somevalue" )
String.replaceFirst( "param1", "somevalue" )

"Cuz" is not a formal word in English. Informally or slangily, its primary
meaning is "cousin". It is also listed as "an attempt to represent the lazy
pronunciation of because."
http://dictionary.reference.com/browse/cuz

--
Lew
From: Daniel Pitts on
On 5/19/2010 2:01 PM, Jimmy wrote:
> Below are the example of possible input strings:
>
> myparam1=myvalue1&param1=value2&param3=value3
> &myparam1=myvalue1&param1=value2&param3=value3
> ?myparam1=myvalue1&param1=value2&param3=value3
> "myparam1=myvalue1&param1=value2&param3=value3"
> "&myparam1=myvalue1&param1=value2&param3=value3"
> "?myparam1=myvalue1&param1=value2&param3=value3"
>
> I like to replace value of "param1" with "somevalue". Can it be done
> in 1 expression replacement? Cuz pattern [\"&?]* works for searching,
> but reusing the same pattern will get rid of the first non-alpha
> character.
Something along the lines of:
str = str.replaceAll("([?&]?)param1=([^&]*)", "$1param1=somevalue");
(untested)
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: markspace on
Daniel Pitts wrote:
> On 5/19/2010 2:01 PM, Jimmy wrote:
>> Below are the example of possible input strings:
>>
>> myparam1=myvalue1&param1=value2&param3=value3
>> &myparam1=myvalue1&param1=value2&param3=value3
>> ?myparam1=myvalue1&param1=value2&param3=value3
>> "myparam1=myvalue1&param1=value2&param3=value3"
>> "&myparam1=myvalue1&param1=value2&param3=value3"
>> "?myparam1=myvalue1&param1=value2&param3=value3"
>>
>> I like to replace value of "param1" with "somevalue". Can it be done
>> in 1 expression replacement? Cuz pattern [\"&?]* works for searching,
>> but reusing the same pattern will get rid of the first non-alpha
>> character.
> Something along the lines of:
> str = str.replaceAll("([?&]?)param1=([^&]*)", "$1param1=somevalue");
> (untested)


Assuming "somevalue" is not really a constant, you might need to use
Matcher.quoteReplacement(String) to get this to work consistently.
Also, is that first ([?&]) bit really needed? It doesn't seem so to me,
but maybe I missed something.


package test;

import java.util.regex.Matcher;

/**
*
* @author Brenden
*/
public class ReplaceTest {
public static void main( String[] args )
{
String test = "&myparam1=myvalue1&param1=value2&param3=value3";
String replace = "some$1value";
test = test.replaceAll( "(myparam1=)[^&]*",
"$1"+Matcher.quoteReplacement( replace ) );
System.out.println( test );
}
}

Output:
run:
&myparam1=some$1value&param1=value2&param3=value3
BUILD SUCCESSFUL (total time: 0 seconds)
From: Daniel Pitts on
On 5/19/2010 3:20 PM, markspace wrote:
> Daniel Pitts wrote:
>> On 5/19/2010 2:01 PM, Jimmy wrote:
>>> Below are the example of possible input strings:
>>>
>>> myparam1=myvalue1&param1=value2&param3=value3
>>> &myparam1=myvalue1&param1=value2&param3=value3
>>> ?myparam1=myvalue1&param1=value2&param3=value3
>>> "myparam1=myvalue1&param1=value2&param3=value3"
>>> "&myparam1=myvalue1&param1=value2&param3=value3"
>>> "?myparam1=myvalue1&param1=value2&param3=value3"
>>>
>>> I like to replace value of "param1" with "somevalue". Can it be done
>>> in 1 expression replacement? Cuz pattern [\"&?]* works for searching,
>>> but reusing the same pattern will get rid of the first non-alpha
>>> character.
>> Something along the lines of:
>> str = str.replaceAll("([?&]?)param1=([^&]*)", "$1param1=somevalue");
>> (untested)
>
>
> Assuming "somevalue" is not really a constant, you might need to use
> Matcher.quoteReplacement(String) to get this to work consistently. Also,
> is that first ([?&]) bit really needed? It doesn't seem so to me, but
> maybe I missed something.
Yes, it is needed so as not to match "myparam1" accidentally.

In reality, even though you can do this in one fell regex swoop, it is
often better to fully parse into a Map<String, List<String>>, and do
your manipulation much more precisely, and then recreate the string. At
least, if you are dealing with HTTP query parameters, that has been my
experience.

>
>
> package test;
>
> import java.util.regex.Matcher;
>
> /**
> *
> * @author Brenden
> */
> public class ReplaceTest {
> public static void main( String[] args )
> {
> String test = "&myparam1=myvalue1&param1=value2&param3=value3";
> String replace = "some$1value";
> test = test.replaceAll( "(myparam1=)[^&]*",
> "$1"+Matcher.quoteReplacement( replace ) );
> System.out.println( test );
> }
> }
>
> Output:
> run:
> &myparam1=some$1value&param1=value2&param3=value3
> BUILD SUCCESSFUL (total time: 0 seconds)


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>