2017-08-04 130 views
-2

在我的搜索过程中,我希望得到关于这个正则表达式的建议。正则表达式转换

从这个测试的表达式:"reporter": "JONES Jack"

我想获得:"reporter":{"name":"jack.jones"}

我尝试用这个表达式:(\"reporter\":\")+(([a-zA-Z]+)[[:space:]]([a-zA-Z]+))

你能建议我的事情,请与Java的的replaceAll?

嗨,

我终于找到了答案:

来形容,第一个表达式的正则表达式是:(\"reporter\"\:\")+([([a-zA-Z]+) (([a-zA-Z]+))\"

对于第二个表达式,正则表达式为:(\"reporter\"\:)+({\"name\":\"[a-zA-Z]+.[a-zA-Z]+\"})

在Java与replaceAll我们有:

output_row.json = output_row.json.replaceAll("(\"reporter\":\")+(([a-zA-Z]+))+\\u0020+(([a-zA-Z]+))\"","\"reporter\":{\"name\":\"$4.$2\"}"); 

啤酒

+0

它看起来像json格式。使用json解析器。不是正则表达式 – Jens

回答

1

假设总是正是格式,并没有任何更多的信息:

正则表达式:"reporter": "(\w+) (\w+)"

替换为:"reporter":{"name":"$2.$1"}

然后,我不认为你可以转换到小写在Java正则表达式所以也许你只是做一个.toLowerCase()