Recupere múltiples datos dentro o {} bloques en una cadena en java

Basically I need to retrieve data inside <> or {} blocks in a string in java.

The String may or may not contain a <> or {} tag.

The string might contain multiple <> or {} blocks.

Data inside the <> and {} blocks are basically variables or function names.

    d1 = "1234";
    d2 = "5678";
    sampleString = "aaa<d1>aaa{d1}aaa<d2>aaa";

I need the result to be similar to something like this.

    resultString = "aaa1234aaa4321aaa5678aaa";

gracias de antemano. :)

EDITAR

I should make note that the values inside the <> and {} blocks are dynamic.

podría tener

    sampleString = "aaa<d5>aaa{d1}aaa<d2>aaa";

meaning it would get the value d5 from a property/variable/parameter/etc.

I should have stated that from the start. :p

preguntado el 04 de julio de 12 a las 07:07

Can the text inside <> y {} contiene <>{}? -

d1 ,d2 are static or dynamic ? -

@Hemant Metalia yes they are dynamic -

5 Respuestas

Well, This is a classic regex task you can solve with Pattern and Matcher in Java. First of all, think about the regex you need to capture this string regardless of Java implementation. something of the sort "(?:.*<(.*?)>.*)|(?:.*{(.*?)}.*)" should help you capture the string. you can use a Matcher to make the match and run in a while loop while .find() method of the Matcher doesn't return NULL (i.e. there's still a match in the string). In the loop replace the needed group.

Respondido 04 Jul 12, 07:07

Podrías simplificar el | out by using character classes [<{] y [>}] although that would match "aaa<d1}aaa" - Zac

agree, I don't see an elegant way to get rid of the pipe.., maybe yuo could lose it and check for even numbers of '{' or '<' but I don't know if that's more efficient than checking it in the regex - wilfo

You could just do 2 different regexes - although thats pretty much what the or does. - Zac

Primero, crea un Map for your values:

Map<String, String> values = new HashMap<String, String>();
values.put("d1", "1234");
values.put("d2", "5678");

Then, find all matches and replace them:

Matcher matcher = Pattern.compile("<(.*?)>").matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(sb, values.get(matcher.group(1)));
}
matcher.appendTail(sb);

And again for the other pattern:

matcher = Pattern.compile("\\{(.*?)\\}").matcher(sb.toString());
sb = new StringBuffer();
while (matcher.find()) {
      matcher.appendReplacement(sb, new StringBuffer(values.get(matcher.group(1))).reverse().toString());
}
matcher.appendTail(sb);

Respondido 04 Jul 12, 07:07

    String d1 = "1234";
    String d2 = "5678";
    String sampleString = "aaa<d1>aaa{d1}aaa<d2>aaa";
    String finalStirng= sampleString.replaceAll("<d1>", d1).replaceAll("\\{d1\\}", d1).replaceAll("<d2>", d2).replaceAll("\\{d2\\}", d2);
    System.out.println("finalStirng= " + finalStirng);

The { and } are special in Java's regex dialect. So you should escape them: as "\\{d1\\}".

Respondido 04 Jul 12, 07:07

Pruebe éste

sampleString = sampleString.replaceAll("<d1>",d1).replaceAll("{d1}",new StringBuffer(d1).reverse().toString()).replaceAll("<d2>",d2);

Respondido 04 Jul 12, 07:07

I don't think @DRTauli knows what is going to be inside of the <> or {} so they can't be directly searched for. - Zac

A direct approach would be using regex as mentioned before

String resultString = "";  
resultString = sampleString.replaceAll("<d1>",d1);  
resultString = resultString.replaceAll("\\{d1\\}",d1);  
resultString = resultString.replaceAll("<d2>",d2);  

please be aware that this would work (though untested) for the specific scenario you've described, but not generic scenarios as the characters between the <> or {} tags might affect the regex. It depends on what is between your <> or {} tags.

Might be better do think about using something like <.*> as your regex condition...

Respondido 04 Jul 12, 07:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.