在java中,很多时候需要解析一段字符串获取符合条件的所有匹配项。下面例子获取一段字符串中满足 ${字母数字} 条件的字符串的代码
import ;
import ;
import ;
public class Test {
public static void main(String[] args) {
String str = "select * from order where createdUser = ${currentUser} and depart = ${currentOrg} and status = 'VALID'";
String reg = "\\$\\{[a-zA-Z0-9]+\\}";//定义正则表达式
Pattern patten = (reg);//编译正则表达式
Matcher matcher = (str);// 指定要匹配的字符串
List<String> matchStrs = new ArrayList<>();
while (()) { //此处find()每次被调用后,会偏移到下一个匹配
(());//获取当前匹配的值
}
for (int i = 0; i < (); i++) {
((i));
}
}
}
代码输出结果:
${currentUser}
${currentOrg}