Regular expression search and replace

J

Jimmy

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.
 
D

Daniel Pitts

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)
 
M

markspace

Daniel 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.
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)
 
D

Daniel Pitts

Daniel 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.
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)
 
E

Eric Sosman

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.
 
L

Lew

Jimmy 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.replaceAll( "param1", "somevalue" )
String.replaceFirst( "param1", "somevalue" )

Eric said:
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.
 
M

markspace

Daniel said:
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 );
}
}
 
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.

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
 
L

Lew

Lew said:
"Cuz" is not a formal word in English.[...]
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.
 
J

Jeff Higgins

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.
 
J

Jeff Higgins

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.

What's the difference between the phrases "the heck with you", and "to
heck with you"?
 
L

Lew

Jeff said:
What's the difference between the phrases "the heck with you", and "to
heck with you"?

None in meaning - I've heard both over the years. They're colloquial, so
variations occur. OTOH, you would utter in astonishment, "The heck you say!"
but not, "To heck you say!"

As for your other post, Jeff, AIUI there are Internet connections in heck, but
only over dialup. Luckily for Arne and Eric, my powers actually to banish
someone to heck are protected by a giant mirror and I only wind up sending
myself there.
 
J

Jeff Higgins

None in meaning - I've heard both over the years. They're colloquial, so
variations occur. OTOH, you would utter in astonishment, "The heck you
say!" but not, "To heck you say!"

I was thinking dismissal and banishment, but either way: Ok.
As for your other post, Jeff, AIUI there are Internet connections in
heck, but only over dialup. Luckily for Arne and Eric, my powers
actually to banish someone to heck are protected by a giant mirror and I
only wind up sending myself there.

Whew! Thank goodness all three will still be able to participate.
 
L

Lew

Interesting, but I fail to see the relevance to Java programming.

Sometimes in this free-speech forum people bring up secondary points. This
one was relevant only insofar as it addressed how someone chose to communicate
in a post to a Java discussion forum. Had the post been to a C# forum it
would have been less relevant to a Java discussion.

Much as Roedy will encourage people to avoid the lazy and, to him, offensive
use of "wanna" for "want to", I sometimes find lazy, stupid and questionable
English usage grating. This does not apply to those doing their best to
express themselves in a foreign language, but simple things like spelling
"Java" correctly, or spelling "you" instead of "u" or "because" instead of
"cuz" just represent a habit of diligence and completeness that spills over
into code.

If someone is too lazy and illiterate to spell "because" or risk
misunderstanding because "cuz" really abbreviates for "cousin", then perhaps
they are careless with Javadocs, or logging, or unit tests, or covering all
possible inputs to a method. OTOH, in the OP's case I am quite sure it was
not laziness or stupidity, but I thought the facts about the word were
interesting enough to post. Furthermore, I anticipated that mentioning those
facts would stimulate discussion and give me the opportunity to put in a pitch
for professionalism and a habit of diligence.
 
C

ClassCastException

Sometimes in this free-speech forum people bring up secondary points.

Don't get all sanctimonious with me. It looked suspiciously to me like an
off-topic criticism of another poster, and its motive was therefore
probably pretty base.
I sometimes find lazy, stupid and questionable English usage grating.

Fine. But it's not the topic of this newsgroup, and on at least some
points of English usage, opinions are liable to differ.
If someone is too lazy and illiterate to spell "because" or risk
misunderstanding because "cuz" really abbreviates for "cousin", then
perhaps they are careless with Javadocs, or logging, or unit tests, or
covering all possible inputs to a method.

This seems like quite a stretch. Fine effort at rationalization, though,
and at trying to shoehorn it in as tangentially on topic. :)
OTOH, in the OP's case I am quite sure it was not laziness or
stupidity, but I thought the facts about the word were interesting
enough to post. Furthermore, I anticipated that mentioning those facts
would stimulate discussion

Well, that it certainly did -- off-topic discussion. Congratulations on
lowering the SNR of this once-fine newsgroup slightly. :)

Now I shall probably endeavor to avoid making it any worse by dropping
this. Unless of course a particularly egregious response gets posted.
 
J

Jim Janney

Jimmy 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");
 
D

Daniel Pitts

Jimmy 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.
 
J

Jim Janney

Daniel Pitts said:
Jimmy 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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top