On 5/20/2010 1:14 PM, Jim Janney wrote:
Daniel Pitts<newsgroup.spamfilter@virtualinfinity.net> writes:
On 5/20/2010 10:26 AM, Jim Janney wrote:
Jimmy<jimmy_please@yahoo.com> writes:
Below are the example of possible input strings:
myparam1=myvalue1¶m1=value2¶m3=value3
&myparam1=myvalue1¶m1=value2¶m3=value3
?myparam1=myvalue1¶m1=value2¶m3=value3
"myparam1=myvalue1¶m1=value2¶m3=value3"
"&myparam1=myvalue1¶m1=value2¶m3=value3"
"?myparam1=myvalue1¶m1=value2¶m3=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 input = "&myparam1=myvalue1¶m1=value2¶m3=value3";
String replaced = input.replaceAll("\\bparam1=\\w+", "param1=somevalue");
Sounds good unless value2 is actually "abc%2C123", in which case \\w+
won't properly match.
Like I've said elsewhere, the least fragile approach is to actually
parse the string and re-create it. Regex hacks might work, but they
might fail in unexpected ways.
Not present in the sample data, but the problem is underspecified.
Agreed, but problems are *often* underspecified in the real
world. While a programmers job may be to implement a exact
specification, an engineers job is to extract that specification from
inexact user requests.
If Jimmy (the OP) only wanted to replace param1=value2 with
param2=somevalue, in the given "sample" data, he could have done so
manually once. It was fairly clear, with some experience in the
field, what his actual problem domain was. It *is* possible that I've
made a mistake, since I've made assumptions about the
request. However, by providing a solution, and explaining the
assumptions, we have provided a path forward in the conversation.
If my assumptions are correct, then my solution will be useful to the
OP. If my assumptions are incorrect, then the OP can correct them and
that process is useful to everyone participating in the conversation.
Agreed, but we're still both guessing. What if&& is supposed to
construct an input that will break it.