java 如何使用正则表达式截取字符串
{"internal_1":[{"version":4,"addr":"192.160.1.11"}]}就那这个json串做例子,用正则表达式获得{"version":4...
{"internal_1": [{"version": 4,"addr": "192.160.1.11"}]}
就那这个json串做例子,用正则表达式获得{"version": 4,"addr": "192.160.1.11"}这个串,用java 展开
就那这个json串做例子,用正则表达式获得{"version": 4,"addr": "192.160.1.11"}这个串,用java 展开
2个回答
展开全部
public static void main(String[]agrs){
String s = "{\"internal_1\": [{\"version\": 4,\"addr\": \"192.160.1.11\"}]}";
String regex = ".+?\\[(.+?)\\].+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
正则表达式:.+?\\[(.+?)\\].+?
结果:{"version": 4,"addr": "192.160.1.11"}
String s = "{\"internal_1\": [{\"version\": 4,\"addr\": \"192.160.1.11\"}]}";
String regex = ".+?\\[(.+?)\\].+?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String group = matcher.group(1);
System.out.println(group);
}else {
System.out.println("no matches!!");
}
}
正则表达式:.+?\\[(.+?)\\].+?
结果:{"version": 4,"addr": "192.160.1.11"}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询