Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular expression search and replace

Reply
Thread Tools

Regular expression search and replace

 
 
Jimmy
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
"?myparam1=myvalue1&param1=value2&param3=value 3"

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.
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
> "?myparam1=myvalue1&param1=value2&param3=value 3"
>
> 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/>
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
>> "?myparam1=myvalue1&param1=value2&param3=value 3"
>>
>> 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=value 3";
String replace = "some$1value";
test = test.replaceAll( "(myparam1=)[^&]*",
"$1"+Matcher.quoteReplacement( replace ) );
System.out.println( test );
}
}

Output:
run:
&myparam1=some$1value&param1=value2&param3=valu e3
BUILD SUCCESSFUL (total time: 0 seconds)
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
>>> "?myparam1=myvalue1&param1=value2&param3=value 3"
>>>
>>> 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=value 3";
> String replace = "some$1value";
> test = test.replaceAll( "(myparam1=)[^&]*",
> "$1"+Matcher.quoteReplacement( replace ) );
> System.out.println( test );
> }
> }
>
> Output:
> run:
> &myparam1=some$1value&param1=value2&param3=valu e3
> BUILD SUCCESSFUL (total time: 0 seconds)



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
>> "?myparam1=myvalue1&param1=value2&param3=value 3"
>>
>> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-19-2010
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=value 3"
>>> "?myparam1=myvalue1&param1=value2&param3=value 3"
>>>
>>> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-19-2010
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
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      05-19-2010
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&para m3=value3";
String replace = "some$1value";
test = test.replaceAll( "((?:^|[&?])param1=)[^&]*",
"$1"+Matcher.quoteReplacement( replace ) );
System.out.println( test );
}
}
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      05-20-2010
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-20-2010
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.


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


Arne Vajhøj wrote:
> 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.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need regular expression to replace part of result based on a search pattern Jimmy Java 13 07-25-2012 09:37 PM
Search, Replace with Regular Expression tushar.n.patel@gmail.com ASP .Net 1 02-09-2005 10:09 PM
Search, Replace with Regular Expression lucky ASP .Net 0 02-09-2005 07:10 AM
regular expression search replace J. VerSchave Java 2 02-29-2004 08:08 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments