Narabhut
Narabhut

Reputation: 837

Alter string using regex in Java

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?

Upvotes: 1

Views: 117

Answers (2)

Pshemo
Pshemo

Reputation: 124215

How about

content.replaceAll("\\bname=\"\\{([^}]+)\\}\"", "name=\"$1\"")

Upvotes: 3

Reuben
Reuben

Reputation: 165

Will this work ?

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

Upvotes: 2

Related Questions