我有一个String,如下所示。它是 NOT JSON
"city":"atlanta","this1":"that1","state":"GA","city":"dallas","this2":"that2","state":"TX"
我的要求是使用正则表达式获取给定州的城市名称。在城市和州参数之间我可能有不同数量的参数。
这个正则表达式模式似乎是贪婪的 - 匹配上面的字符串
的亚特兰大城市"city":"(.*?)".*?"state":"TX"
匹配
"city":"atlanta","this1":"that1","state":"GA","city":"dallas","this2":"that2","state":"TX"
我正在寻找这样的比赛。
"city":"dallas","this2":"that2","state":"TX"
如何仅在TX中匹配?
答案 0 :(得分:0)
为什么你不尝试这样的东西,这看起来比正则表达式容易得多。
public class TestStack {
public static void main(String[] args) {
String s="\"city\":\"atlanta\",\"this1\":\"that1\",\"state\":\"GA\",\"city\":\"dallas\",\"this2\":\"that2\",\"state\":\"TX\"";
String[] arr=s.split(",");
for(int i=0;i<arr.length;i++){
if(arr[i].contains("city")||arr[i].contains("state")){
System.out.println(arr[i]);
}
}
}
}