# Using Regex in Java
以下使用到的类 from java.util.regex
# 示例
// 目标内容,想要提取 status 的值 limit。
String str = "UNKNOWN: 1000999\n\nPurchase failed\n\n{\"status\":\"limit\"}"
// 编写正则表达式
String regex = "\\{\"status\":\"(.+)\"}";
// 创建样板
Pattern pattern = Pattern.compile(regex);
// 使用样板去匹配内容 str
Matcher matcher = pattern.matcher(str);
// 执行匹配操作, find() 是匹配子串,matches() 是匹配整个 str
// find(start_index) 接收一个参数表示从 str 的哪一位开始匹配。
matcher.find();
// 获取捕获到的内容
String limit = matcher.group(1);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 讨论区
由于评论过多会影响页面最下方的导航,故将评论区做默认折叠处理。
点击查看评论区内容,渴望您的宝贵建议~
← Regex