1

I have a XML file loaded into a String like this

<children name="{content.type}">
    <values>{video}</values>
</children>
<children name="{content.size}">
    <values>250</values>
</children>
<children name="uploaded by">
    <values>user1</values>
</children>

I want to remove the {} in the name tags so the output looks like this

<children name="content.type">
    <values>{video}</values>
</children>
<children name="content.size">
    <values>250</values>
</children>
<children name="uploaded by">
    <values>user1</values>
</children>

Currently I have this code -

Pattern p = Pattern.compile("([^,]*)\"\\{([^,]*)\\}\"([^,]*)");
Matcher m = p.matcher(content);
if (m.find()){
    System.out.println(m.group(1));
}

But the string gets cutoff midway. Is something wrong with my regex?

2
  • 1
    Don't you need to concatenate the second and third group? You are printing only the first captured group right now I think. Commented Jun 18, 2013 at 17:32
  • Haha I feel like an idiot. Thanks for catching this! Commented Jun 18, 2013 at 17:36

2 Answers 2

3

How about

content.replaceAll("\\bname=\"\\{([^}]+)\\}\"", "name=\"$1\"")
Sign up to request clarification or add additional context in comments.

Comments

2

Will this work ?

s.replaceAll( "name=\"\\{", "name=\"" ).replaceAll( "\\}\">", "\">" )

1 Comment

This will work only if OP wont have other attributes in his input like <children name="{content.type}" someOtherAttrib="{some.value}">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.