正则匹配
public static boolean isLegalInputLine(String line) {
// 定义正则
Pattern p =Pattern.compile("GraphType\\s*=\\s*\".+\"\\s*");
// 进行匹配
Matcher m = p.matcher(line);
// 获取结果
boolean r =m.matches();
return r;
}
提取字符串
public List<String> getString(String s) {
List<String> strs = new ArrayList<String>();
// 定义正则
Pattern p = Pattern.compile("GraphType\\s*=\\s*\".+\"\\s*");
// 进行匹配
Matcher m = p.matcher(s);
// 提取字符串
while(m.find()) {
strs.add(m.group());
}
return strs;
}
Java正则匹配GraphType字符串,

这段代码展示了如何在Java中使用正则表达式进行字符串匹配和提取。方法isLegalInputLine检查输入字符串是否符合GraphType=某值的格式,而getString方法则从给定字符串中提取所有符合该格式的子串。
2109

被折叠的 条评论
为什么被折叠?



