Java正则表达式对模式中的字符串部分求反
下面是一个基本的java方法,我试图从中找到一个模式Java正则表达式对模式中的字符串部分求反,java,regex,Java,Regex,下面是一个基本的java方法,我试图从中找到一个模式 public static void stringFilter() { int compVal = 33331; List<String> strList = new ArrayList<String>() {{ add("app_Usage_RTIS/batch_id=11111/abc"); add("ENV_RTIS/batch_id=22222/");
public static void stringFilter() {
int compVal = 33331;
List<String> strList = new ArrayList<String>() {{
add("app_Usage_RTIS/batch_id=11111/abc");
add("ENV_RTIS/batch_id=22222/");
add("ABCD-EFG_RTIS/batch_id=33333/");
add("/RTIS/batch_id=44444/");
add("/batch_id=55555/");
}};
Pattern pattern = Pattern.compile(".*_RTIS/batch_id=(\\d+)/.*");
for (String s : strList) {
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
System.out.println(s + "\tTrue");
}
}
}
但这对我不起作用
为了清楚起见,我的输出应该只选择->
"/RTIS/batch_id=44444/" True
"/batch_id=55555/" True
提前谢谢。您就快到了。您可以使用
(?要求在/batch\u id=(\\d+)
部分之前,没有与\u RTI
匹配的项
所以你的正则表达式看起来像:“*”(?谢谢..它对我很有效。你能说明一下“@prabhulingappas”的用法吗?不客气,但我不确定我是否能比链接文章更好地解释它。如果在阅读了该教程后,仍然不清楚,请询问具体部分。
"/RTIS/batch_id=44444/" True
"/batch_id=55555/" True
List<String> strList = Arrays.asList(
"app_Usage_RTIS/batch_id=11111/abc",
"ENV_RTIS/batch_id=22222/",
"ABCD-EFG_RTIS/batch_id=33333/",
"/RTIS/batch_id=44444/",
"/batch_id=55555/"
);
Pattern pattern = Pattern.compile(".*(?<!_RTIS)/batch_id=(\\d+)/.*");
for (String s : strList) {
if (pattern.matcher(s).matches()) {
System.out.println(s + "\tTrue");
}
}
/RTIS/batch_id=44444/ True
/batch_id=55555/ True