public void getPhoneStr(String jsonString, Set<String> set) {
String regex = "\"phone\":\"(.*?)\\\"";//使用非贪婪模式
Matcher matcher = Pattern.compile(regex).matcher(jsonString);
while (matcher.find()) {
set.add(matcher.group(1));
}
}
public static void main(String[] args) {
String jsonString = "{\"page_size\":\"12321321\",\"page_size\":\"123\",\"page_index\":0,\"page_count\":0,\"has_next\":false,\"datas\":null,\"code\":0,\"issuccess\":true,\"msg\":null}";
String regex = "\"page_size\":\"(.*?)\\\"";//使用非贪婪模式
Matcher matcher = Pattern.compile(regex).matcher(jsonString);
while (matcher.find()) {
String ret = matcher.group(1);
System.out.println("key:" + matcher.group(1));
}
String str = "{\"keyword\":\"xin \"hua\",\"type\":\"news\",\"countryName\":\"not中国\"," +
"\"pageNo\":1,\"pageSize\":10,\"beginDate\":\"\"\"2019-11-23 23:59:59\"\"\"," +
"\"endDate\":\"2019-11-23 23:59:59\"}";
Pattern pattern = Pattern.compile("\"([a-zA-z0-9]{0,})\":\"{1}([a-zA-z0-9\\-\\s\\:\\u4e00-\\u9fa5\"]{0,})\"{1}[\\,\\}]{1}|" +
"\"([a-zA-z0-9]{0,})\":([a-zA-z0-9\\-\\s\\:\\u4e00-\\u9fa5\"]{0,})[\\,\\}]{1}");
Matcher matcher1 = pattern.matcher(str);
while (matcher1.find()){
if(matcher1.group(1)!=null) {
//System.out.println("key:" + matcher1.group(1));
System.out.println("value:" + matcher1.group(2));
}else{
// System.out.println("key:" + matcher1.group(3));
System.out.println("value:" + matcher1.group(4));
}
}
}
返回结果
value |
---|
xin “hua |
news |
not中国 |
1 |
10 |
”“2019-11-23 23:59:59"” |
2019-11-23 23:59:59 |