BeginnerPro
BeginnerPro

Reputation: 85

Encoding URL strings with regular expression

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.

Upvotes: 2

Views: 6723

Answers (6)

Brian Jenkins
Brian Jenkins

Reputation: 11

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.

Upvotes: 1

fmucar
fmucar

Reputation: 14558

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

Upvotes: 2

dweeves
dweeves

Reputation: 5615

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

Upvotes: 1

MatBanik
MatBanik

Reputation: 26870

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

Upvotes: 7

Miserable Variable
Miserable Variable

Reputation: 28761

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.

Upvotes: 1

adarshr
adarshr

Reputation: 62603

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?

Upvotes: 3

Related Questions