2

I'm trying to replace several different characters with different values. For example, if I have: #love hate then I would like to do is get back %23love%20hate

Is it something to do with groups? i tried to understand using groups but i really didn't understand it.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
BeginnerPro
  • 85
  • 2
  • 4

6 Answers6

7

You can try to do this:

String encodedstring = URLEncoder.encode("#love hate","UTF-8");

It will give you the result you want. To revers it you should do this:

String loveHate = URLDecoder.decode(encodedstring);
MatBanik
  • 26,356
  • 39
  • 116
  • 178
3

You don't need RegEx to replace single characters. RegEx is an overkill for such porposes. You can simply use the plain replace method of String class in a loop, for each character that you want to replace.

String output = input.replace("#", "%23");
output = output.replace(" ", "%20");

How many such characters do you want to get replaced?

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • Thanks for the reply, well i may need to repalce several characters like if i have: #i love you then the output would have to be %23i%20love%20you. and thank you ill have a look at it – BeginnerPro Jan 07 '11 at 16:09
  • Np. But having even tens of such statements is far more efficient than using a single RegEx compile. – adarshr Jan 07 '11 at 16:12
2

If you are trying to encode a URL to utf-8 or some encoding using existing classes will be much easier

eg.

commons-httpclient project

  URIUtil.encodeWithinQuery(input,"UTF-8");
fmucar
  • 14,361
  • 2
  • 45
  • 50
1

No, you will need multiple replaces. Another option is to use group to find the next occurrence of one of several strings, inspect what the string is and replace appropriately, perhaps using a map.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
1

i think what you want to achieve is kind of url encoding instead of pure replacement.

see some answers on this thread of SO , especially the one with 7 votes which may be more interesting for you.

HTTP URL Address Encoding in Java

Community
  • 1
  • 1
dweeves
  • 5,525
  • 22
  • 28
1

As Mat said, the best way to solve this problem is with URLEncoder. However, if you insist on using regex, then see the sample code in the documentation for java.util.regex.Matcher.appendReplacement:

 Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

Within the loop, you can use m.group() to see what substring matched and then do a custom substitution based on that. This technique can be used for replacing ${variables} by looking them up in a map, etc.