-1

I have a string of SVG markup that contains multiples of these:

url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)

and I need them to be like this:

url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')

with quotes inside the parenthesis.

These will be mixed inside a long string containing lots of different markup, so needs to be very accurate.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

You can use a regex like this:

\((.*?)\)

With the replacement string ('$1')

The idea is capture everything within parentheses and concatenates the '

So, you can use a code like this:

String str = "url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)";
str = str.replaceAll("\\((.*?)\\)", "('$1')");

//Outuput: url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')

IdeOne example

In case you want a better performance regex you can use:

str = str.replaceAll("\\(([^)]*)\\)", "('$1')");
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • @frederico Thanks for the reply...it needs to only do it for parentheses that have url in front of them, So only hit url() and NOT rgb() – David Johnson Jan 29 '16 at 15:23
  • @DavidJohnson I don't see other parentheses in your example. Anyway, saw you accepted the answer. Hope to help – Federico Piazza Jan 29 '16 at 15:25
  • I did say 'These will be mixed inside a long string containing lots of different markup, so needs to be very accurate.' So inside this markup is other parentheses – David Johnson Jan 29 '16 at 15:28
  • @DavidJohnson sorry, I see... in that case you can use this line: `str = str.replaceAll("\\((.*)\\)", "('$1')");` to make the regex greedy. Bear in mind that this regex will treat `url` as the biggest node. If that doesn't work for you, then you can't use regex. – Federico Piazza Jan 29 '16 at 15:29
  • str.replaceAll("\\((.*)\\)", "('$1')") only gets the first and last parentheses in the whole string – David Johnson Jan 29 '16 at 16:24
  • @DavidJohnson then you can't use regex for what you want to do – Federico Piazza Jan 29 '16 at 17:21
  • this worked in the end: str.replaceAll("url\\('(.*?)'\\)", "url($1)") , I just added the 'url' bit...so your answer was correct apart from that...cheers – David Johnson Feb 01 '16 at 13:48
  • @DavidJohnson great, glad to help – Federico Piazza Feb 01 '16 at 15:41
0

ReplaceAll remove a part of the string and put an unrelated and invariant new stuff instead.

Because the replacement string can't be the same at both side, the only solution I imagine (with the constraint of using RegEx and ReplaceAll) is to do it in two time:

String Str = "url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)";
Str = Str.replaceAll("\\(", "('"); // replace left parenthesis
Str = Str.replaceAll("\\)", "')"); // replace right parenthesis

System.out.print("Return Value: " + Str);
// Return Value: url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')

You can test it here.

Orace
  • 7,822
  • 30
  • 45