简体   繁体   中英

strip and replace a text string with a regular expression in Java

I am trying to strip and replace a text string in the most elegant way possible:

With the solution I have /element\\s*\\{"([^"]+)"\\}\\s*{text\\s*{\\s*}\\s*({[^}]*})/

text.replaceAll("element\\s*\\{\"([^\"]+)\"}\\s*\\{text\\s*\\{\\s*}\\s*(\\{[^}]*})", "<$1> $2"));

Used on the text below:

element {"item"} {text { } {$i/child::itemno} text { } {$i/child::description} text { } element {"high_bid"} {{max($b/child::bid)}} text { }} 

GIVES:

<item> {$i/child::itemno} text { } {$i/child::description} text { } element {"high_bid"} {{max($b/child::bid)}} text { }}

When I'm trying to achieve:

<item>{$i/child::itemno}{$i/child::description}<high_bid>{fn:max($b/child::bid)}</high_bid></item> 

After reviewing, the problem is that the regex only matches once.

Your regex is looking for element{"tag"} {text { } {text_here}

This only occurs once in your input:

element {"item"} {text { } {$i/child::itemno}

Nothing else matches:

text { } element {"high_bid"} {   => NO MATCH, text without element before it

element {"high_bid"} {{max($b/child::bid)}} text { }   => NO MATCH, text after braces

So either your input is bad, or you need something better than a one-shot regex.

That being said, I don't think a regex will work here. You could remove all of the "text { }" elements, which seem to do nothing:

text.replaceAll("text\\s*\\{\\s*}", ""));

Which gives you:

element {"item"} { {$i/child::itemno}  {$i/child::description}  element {"high_bid"} {{max($b/child::bid)}} }

But the problem here is that you have nesting. If you are simply matching on braces, how do you know how far to match? You need your regex to comprehend how many opening braces you have, and find the correct closing brace. This is not really doable with regular expressions. You need a function that parses the string counting opening braces and subtracting closing braces. When you get a count of zero, you found a set... Of course, this is not regular expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM