我试图剥离和替换文本字符串,看起来最优雅的方式可能如下:最优雅的方式来剥离和替换字符串模式
element {"item"} {text {
} {$i/child::itemno}
的样子:
<item> {$i/child::itemno}
因此删除替代其大括号的元素文本并删除文本及其伴随的大括号。这些模式可能会多次出现。我最好使用Java的java.util.regex.Pattern或简单的replaceAll或org.apache.commons.lang.StringUtils?
谢谢你的反应变量:
我现在有以下,但我不能确定为反斜杠的数量,以及如何完成最终的替代,这使得我的组(1)的使用和<替换它在其开始和>在其结束:
Pattern p = Pattern.compile("/element\\s*\\{\"([^\"]+)\"\\}\\s*{text\\s*{\\s*}\\s*({[^}]*})/ ");
// Split input with the pattern
Matcher m = p.matcher("element {\"item\"} {text {\n" +
" } {$i/child::itemno} text { \n" +
" } {$i/child::description} text {\n" +
" } element {\"high_bid\"} {{max($b/child::bid)}} text {\n" +
" }} ");
// For each instance of group 1, replace it with < > at the start and end
对不起,我是新来的正则表达式,如何能这是合并成一条线? – Pablo