[英]negate and replacing string with regex in java backslash and quate
[英]Java RegEx to negate part of the string in a pattern
下面是我试图从中找到一种模式的基本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/");
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=123***/"
字符串,但是我的想法是仅选择不包含"_RTIS"
字符串,但可以包含“ / batch_id = 123 *” ** /”。 我试着做
`Pattern pattern = Pattern.compile(".*(!_RTIS)/batch_id=(\\d+)/.*");`
但这对我没有用。
为了清楚起见,我的输出应仅选择->
"/RTIS/batch_id=44444/" True
"/batch_id=55555/" True
提前致谢。
您要关闭。 您可以使用/batch_id=(\\\\d+)
-look-behind (?<!...)
要求/batch_id=(\\\\d+)
部分之前不存在_RTIS
匹配_RTIS
。
因此,您的正则表达式应类似于: ".*(?<!_RTIS)/batch_id=(\\\\d+)/.*"
。
演示:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.