如何迭代正则表达式
假设我有以下字符串:
name1=gil;name2=orit;
我想找到的所有匹配项,并确保整个字符串与模式匹配。name=value
所以我做了以下事情:
-
确保整个模式与我想要的匹配。
Pattern p = Pattern.compile("^((\\w+)=(\\w+);)*$"); Matcher m = p.matcher(line); if (!m.matches()) { return false; }
-
迭代模式
name=value
Pattern p = Pattern.compile("(\\w+)=(\\w+);"); Matcher m = p.matcher(line); while (m.find()) { map.put(m.group(1), m.group(2)); }
有没有办法用一个正则表达式做到这一点?