正则表达式替换块

时间:2016-02-02 02:22:43

标签: java regex

我有一个包含重复部分的字符串:

"dim": [{
          "name": "1",
          "value": "43.8"
        }, {
          "name": "2",
          "value": "27.9"
        }, {
          "name": "3",
          "value": "36.7"
       }]

也就是说,有多个" dim"块,每个块可能包含" name"," value"标签

我需要将所有这些到位转换为:

"1" : "43.8",
"2" : "27.9",
"3" : "36.7"

在java中是否有一个简单的正则表达式?

1 个答案:

答案 0 :(得分:0)

StringBuilder text = new StringBuilder("\"dim\": [{\n"
        + "\t\t\"name\": \"1\",\n"
        + "\t\t\"value\": \"43.8\"\n"
        + "\t}, {\n"
        + "\t\t\"name\": \"2\",\n"
        + "\t\t\"value\": \"27.9\"\n"
        + "\t}, {\n"
        + "\t\t\"name\": \"3\",\n"
        + "\t\t\"value\": \"36.7\"\n"
        + "\t}]");
Pattern p = Pattern.compile( "(?ms)(?:\\\"dim\\\"\\:\\s*\\[)?\\{\\s*\\\"name\\\"\\:\\s*(\\\".*?\\\")\\,\\s*\\\"value\\\"\\:\\s*(\\\".*?\\\")\\s*\\}(\\,?\\s*)(?:\\])?" );
Matcher m = p.matcher( text );
while( m.reset( text ).find() ) // since we are changing in-place we have to update the matcher every time or else the indexes will be wrong, which is also why we must do our replacements from the back
{
    if( !m.group( 3 ).isEmpty() )
        text.replace( m.start( 3 ), m.end( 3 ), ",\n" ); // format the separating commas
    else
        text.delete( m.start( 3 ), m.end() ); // delete the trailing ]
    text.delete( m.end( 2 ), m.start( 3 ) ); // delete the beginning of the {
    text.replace( m.end( 1 ), m.start( 2 ), " : " ); // set the separator
    text.delete( m.start(), m.start( 1 ) ); // trim off the end of the }
}
System.out.println( text );