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"}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询