From: Eric Sosman on
On 5/19/2010 5:14 PM, Lew wrote:
> 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" )

Not what he wants. He's trying to replace "value of `param1'",
which I understand to mean he wants "&param1=value2" to become
"&param1=somevalue". Your snippet would instead change it to
"&somevalue=value2". Also, it would change "&param12=separam13"
to "&somevalue2=sesomevalue3".

> "Cuz" is not a formal word in English.[...]

Lew's "Three R's" must have been Reading, Witing, and Ranting.
God willing, someday he'll Reduce, Reuse, and Recycle the last.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Lew on
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.

Lew wrote:
>> String.replaceAll( "param1", "somevalue" )
>> String.replaceFirst( "param1", "somevalue" )

Eric Sosman wrote:
> Not what he wants. He's trying to replace "value of `param1'",
> which I understand to mean he wants "&param1=value2" to become
> "&param1=somevalue". Your snippet would instead change it to
> "&somevalue=value2". Also, it would change "&param12=separam13"
> to "&somevalue2=sesomevalue3".

Oh, my mistake.

Then he wants to set up capture groups for replacement along the lines Daniel
suggested.

--
Lew
From: Lew on
Lew wrote:
>> "Cuz" is not a formal word in English.[...]

Eric Sosman wrote:
> Lew's "Three R's" must have been Reading, Witing, and Ranting.
> God willing, someday he'll Reduce, Reuse, and Recycle the last.

Was there anything at all inaccurate in my assertion? If not, then the heck
with you.

--
Lew
From: markspace on
Daniel Pitts wrote:

> Yes, it is needed so as not to match "myparam1" accidentally.


Ah, I misread the problem statement. Your's doesn't work though,
because ? matches zero or more, and there is definitely zero &? in the
middle of "myparam1".

Add a "or start of line" and omit the ? however and it works, at least
in my test.

package test;

import java.util.regex.Matcher;

/**
*
* @author Brenden
*/
public class ReplaceTest {
public static void main( String[] args )
{
String test =
"param1=test2&myparam1=myvalue1&param1=value2&param3=value3";
String replace = "some$1value";
test = test.replaceAll( "((?:^|[&?])param1=)[^&]*",
"$1"+Matcher.quoteReplacement( replace ) );
System.out.println( test );
}
}
From: Arne Vajhøj on
On 19-05-2010 18:45, Lew wrote:
> Lew wrote:
>>> "Cuz" is not a formal word in English.[...]
>
> Eric Sosman wrote:
>> Lew's "Three R's" must have been Reading, Witing, and Ranting.
>> God willing, someday he'll Reduce, Reuse, and Recycle the last.
>
> Was there anything at all inaccurate in my assertion? If not, then the
> heck with you.

I am sure that your description of what "Cuz" means
is accurate.

But that does not necessarily make it worthwhile
to bring up in a Java programming forum.

I can understand complaints about form when a post is
hardly recognizable as English.

But this one was not so bad.

Arne