From: Jimmy on 19 May 2010 17:01 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.
From: Lew on 19 May 2010 17:14 On 05/19/2010 05:01 PM, Jimmy wrote: > 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.replaceAll( "param1", "somevalue" ) String.replaceFirst( "param1", "somevalue" ) "Cuz" is not a formal word in English. Informally or slangily, its primary meaning is "cousin". It is also listed as "an attempt to represent the lazy pronunciation of because." http://dictionary.reference.com/browse/cuz -- Lew
From: Daniel Pitts on 19 May 2010 17:39 On 5/19/2010 2:01 PM, Jimmy wrote: > 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. Something along the lines of: str = str.replaceAll("([?&]?)param1=([^&]*)", "$1param1=somevalue"); (untested) -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: markspace on 19 May 2010 18:20 Daniel Pitts wrote: > On 5/19/2010 2:01 PM, Jimmy wrote: >> 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. > 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¶m1=value2¶m3=value3"; String replace = "some$1value"; test = test.replaceAll( "(myparam1=)[^&]*", "$1"+Matcher.quoteReplacement( replace ) ); System.out.println( test ); } } Output: run: &myparam1=some$1value¶m1=value2¶m3=value3 BUILD SUCCESSFUL (total time: 0 seconds)
From: Daniel Pitts on 19 May 2010 18:28 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¶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. >> 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¶m1=value2¶m3=value3"; > String replace = "some$1value"; > test = test.replaceAll( "(myparam1=)[^&]*", > "$1"+Matcher.quoteReplacement( replace ) ); > System.out.println( test ); > } > } > > Output: > run: > &myparam1=some$1value¶m1=value2¶m3=value3 > BUILD SUCCESSFUL (total time: 0 seconds) -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: Exception classes (Was: entity bean as being reentrant) Next: A little afternoon source code |