json数据都有非常多非常好的解析工具,如果是javascript,甚至都是内置的,不建议用正则,
原因:
1. 正则易写不易维护;
2. 总有你测试不到的特殊情况;
3. 不通用,如果你想得到更多字段的数据, 正则不适合做这件事.
不过,既然一定要,可以试试这个:
js:
SyncKey = {"Count": 4,"List": [{"Key": 1,"Val": 694398478},{"Key": 2,"Val": 694398818},{"Key": 3,"Val": 694398792},{"Key": 1000,"Val": 1525475042}]}
res = JSON.stringify(SyncKey)
var myregexp = /"Val":(\d+)/g;
var match = myregexp.exec(res);
while (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
console.log(match[1])
match = myregexp.exec(res);
}
Java:
import java.util.regex.*;
public class RegTest{
public static void main(String[] args){
String SyncKey = "{\"Count\": 4,\"List\": [{\"Key\": 1,\"Val\": 694398478},{\"Key\": 2,\"Val\": 694398818},{\"Key\": 3,\"Val\": 694398792},{\"Key\": 1000,\"Val\": 1525475042}]}";
Pattern p = Pattern.compile("\"Val\":\\s*(\\d+)");
Matcher m = p.matcher(SyncKey);
while (m.find()) {
System.out.println(m.group(1));
}
}
}