Regular expression search and replace

D

Daniel Pitts

Daniel Pitts said:
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 input = "&myparam1=myvalue1&param1=value2&param3=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.
 
J

Jim Janney

Daniel Pitts said:
Daniel Pitts said:
On 5/20/2010 10:26 AM, Jim Janney 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 input = "&myparam1=myvalue1&param1=value2&param3=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
represent a quoted & in a value? (Probably not, but who knows?)
Without some sort of ground rules, I can take any solution and
construct an input that will break it.
 
D

Daniel Pitts

Daniel Pitts said:
On 5/20/2010 10:26 AM, Jim Janney 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 input = "&myparam1=myvalue1&param1=value2&param3=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
represent a quoted& in a value? (Probably not, but who knows?)
Without some sort of ground rules, I can take any solution and
construct an input that will break it.
True, but my assumptions are that this is a standard URL, encoded with
the standard %xx notation (rather than &&). Considering the prevalence
of web technology (esp. with Java programming), and the frequency I've
come across a similar problem, I would be more apt to believe it was as
I assumed, than any other way, until proven otherwise (by the OP
enlightening us all).
 
A

Arne Vajhøj

Lew said:
"Cuz" is not a formal word in English.[...]
Eric said:
Lew's "Three R's" must have been Reading, Witing, and Ranting.
God willing, someday he'll Reduce, Reuse, and Recycle the last.
Lew:
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.

Hey, if the poster in question wants to sound like a stupid teenager
with no skills, that's their business, but they should at least be aware
of it. How many micro-ergs of energy did they save by shaving off four
characters? To me it's an indication of likely carelessness in coding
style - I mean, if you can't even be bothered to spell out "because",
how likely are they to include full Javadocs and logging in their code?

You can be an apologist for laziness and stupid txtsp33k if you like,
but someone's got to take a stand. "Cuz" that's just how I roll, cuz.
Now to heck with you.

I can understand the point if the question did sound like a
stupid teenager, but it did not.

It was readable English - maybe not native English (I am not
qualified to judge that) but easily readable English.

Cuz is not Oxford English but it seems to be a known
style.

http://en.wiktionary.org/wiki/because

(interestingly http://en.wikipedia.org/wiki/Cuz has
a term for the phenomenon "apocopation")

Readable English is a good thing for questions posted here.

Formal English seems like a tough requirement.

Arne
 
A

Arne Vajhøj

Lew said:
"Cuz" is not a formal word in English.[...]

Eric said:
Lew's "Three R's" must have been Reading, Witing, and Ranting.
God willing, someday he'll Reduce, Reuse, and Recycle the last.
Lew:
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.

Hey, if the poster in question wants to sound like a stupid teenager
with no skills, that's their business, but they should at least be aware
of it. How many micro-ergs of energy did they save by shaving off four
characters? To me it's an indication of likely carelessness in coding
style - I mean, if you can't even be bothered to spell out "because",
how likely are they to include full Javadocs and logging in their code?

You can be an apologist for laziness and stupid txtsp33k if you like,
but someone's got to take a stand. "Cuz" that's just how I roll, cuz.
Now to heck with you.

I'm sorry that Eric and Arne have been banished to heck. I hope they
will be able to still participate in this group's discussions from
there, otherwise they will be sadly missed.

Maybe there are good internet connections in heck ...

:)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top