使用或当Java代码中存在两个组时验证正则表达式组
需要验证一种格式,其中可以存在传输和协议,也可以同时存在传输和协议。我已经应用了这个正则表达式来验证这两个组。但是,当任何一个验证成功时,它都不会验证其他组,就像在代码中一样。 注意:传输可以接受tcp、sctp或udp,协议可以接受radius、diameter和tacacs+ 与示例类似,如果传输和协议都是URL的一部分,并且如果传输格式正确并且使用Regex中提供的值进行验证,那么即使协议的值无效,它也会返回true Java代码:使用或当Java代码中存在两个组时验证正则表达式组,java,regex,validation,regex-lookarounds,regex-group,Java,Regex,Validation,Regex Lookarounds,Regex Group,需要验证一种格式,其中可以存在传输和协议,也可以同时存在传输和协议。我已经应用了这个正则表达式来验证这两个组。但是,当任何一个验证成功时,它都不会验证其他组,就像在代码中一样。 注意:传输可以接受tcp、sctp或udp,协议可以接受radius、diameter和tacacs+ 与示例类似,如果传输和协议都是URL的一部分,并且如果传输格式正确并且使用Regex中提供的值进行验证,那么即使协议的值无效,它也会返回true Java代码: String pattern = "((?=.*;tra
String pattern = "((?=.*;transport=(tcp|sctp|udp)\\b)|(?=.*;protocol=(diameter|radius|tacacs+)\\b))";
String url = "transport=tcp;protocol=aradius";
Pattern regExPattern = Pattern.compile(pattern);
if(regExPattern.matcher(url).find()) {
return true;
} else {
return false;
}
这返回true,因为它成功验证了传输,但无法验证协议值。我不能100%确定哪些字符串应被检测为有效,但让我尝试给出一个示例:
import java.util.regex.Pattern;
public class Main
{
private static void check(String s,Pattern regExPattern)
{
boolean matches=regExPattern.matcher(s).matches();
System.out.println(s);
System.out.println("matches:"+regExPattern.matcher(s).matches()+", find:"+regExPattern.matcher(s).find());
System.out.println();
}
public static void main(String[] args) throws Exception
{
String pattern = "((.+=.+;transport=(tcp|sctp|udp))||(.+=.+;protocol=(diameter|radius|tacacs\\+)))";
Pattern regExPattern = Pattern.compile(pattern);
check("transport=tcp;protocol=diameter", regExPattern);
check("transport=udp", regExPattern);
check("protocol=radius", regExPattern);
check("other=other;protocol=radius", regExPattern);
check("other=other;transport=sctp", regExPattern);
check("wrong;protocol=tacacs+", regExPattern);
check("wrong;transport=tcp", regExPattern);
check("wrong;wrong", regExPattern);
check("something else;transport=tcp;protocol=diameter;something else", regExPattern);
}
}
产出:
transport=tcp;protocol=diameter
matches:true, find:true
transport=udp
matches:false, find:true
protocol=radius
matches:false, find:true
other=other;protocol=radius
matches:true, find:true
other=other;transport=sctp
matches:true, find:true
wrong;protocol=tacacs+
matches:false, find:true
wrong;transport=tcps
matches:false, find:true
wrong;wrong
matches:false, find:true
something else;transport=tcp;protocol=diameter;something else
matches:false, find:true
transport=tcp;protocol=diameter
matches
transport=udp
does not match
protocol=radius
does not match
other=other;protocol=radius
matches
other=other;transport=sctp
matches
wrong;protocol=tacacs+
matches
wrong;transport=tcp
matches
wrong;wrong
does not match
something else;transport=tcp;protocol=diameter;something else
matches
我将?
替换为+
,因为?
只匹配一个字符,+
匹配多个字符。然后可以使用match()
而不是find()
match()要求整个字符串与模式匹配。find()只要求字符串的一部分与模式的一部分匹配。我想你想要匹配(而不是找到)
我将*
替换为+
,因为*
匹配任意数量的字符(包括零个字符),但我假设您在这里只想匹配至少一个字符
我删除了\\b
,因为它与字符b
完全匹配,这似乎不是您想要的
我将tacacs+
替换为tacacs\\\+
,因为您希望匹配+
,而不是匹配所有以tacacs
开头的单词和任何数量的s
如tacacsss
该网站对测试正则表达式非常有用,因为它用颜色标记图案的部分,并解释这些部分的含义
不要使用您不理解的复杂表达式,您可以单独检查这两个部分,然后编写if(matches | | matches2).
语句,检查两个部分中是否至少有一个匹配:
import java.util.regex.Pattern;
public class Main
{
private static void check(String s, String pattern1, String pattern2)
{
System.out.println(s);
boolean matches1 = s.matches(pattern1);
boolean matches2 = s.matches(pattern2);
if (matches1 || matches2)
{
System.out.println("matches");
}
else
{
System.out.println("does not match");
}
}
public static void main(String[] args) throws Exception
{
String pattern1 = ".*;transport=(tcp|sctp|udp).*";
String pattern2 = ".*;protocol=(diameter|radius|tacacs\\+).*";
check("transport=tcp;protocol=diameter", pattern1, pattern2);
check("transport=udp", pattern1, pattern2);
check("protocol=radius", pattern1, pattern2);
check("other=other;protocol=radius", pattern1, pattern2);
check("other=other;transport=sctp", pattern1, pattern2);
check("wrong;protocol=tacacs+", pattern1, pattern2);
check("wrong;transport=tcp", pattern1, pattern2);
check("wrong;wrong", pattern1, pattern2);
check("something else;transport=tcp;protocol=diameter;something else", pattern1, pattern2);
}
}
产出:
transport=tcp;protocol=diameter
matches:true, find:true
transport=udp
matches:false, find:true
protocol=radius
matches:false, find:true
other=other;protocol=radius
matches:true, find:true
other=other;transport=sctp
matches:true, find:true
wrong;protocol=tacacs+
matches:false, find:true
wrong;transport=tcps
matches:false, find:true
wrong;wrong
matches:false, find:true
something else;transport=tcp;protocol=diameter;something else
matches:false, find:true
transport=tcp;protocol=diameter
matches
transport=udp
does not match
protocol=radius
does not match
other=other;protocol=radius
matches
other=other;transport=sctp
matches
wrong;protocol=tacacs+
matches
wrong;transport=tcp
matches
wrong;wrong
does not match
something else;transport=tcp;protocol=diameter;something else
matches
复杂的是,传输和协议都是可选的,并且以任何顺序出现。为了在一个正则表达式中实现这一点,我使用
|
进行了一个替换,其中在第一个替换中,首先扫描传输参数,协议参数是可选的,在第二个替换中,事情是相反的。要实现一个可选的正则表达式A
,它可以出现在输入流中很远的地方,我的方法是使用:
(.*A)|)
如果正则表达式A
出现在输入流中,请扫描A,否则不扫描字符。但不管怎样,都有一场比赛
您不能使用:
*(?A)?
因为正则表达式引擎通过扫描0个字符而从不扫描A
来满足这一点
正则表达式:
;transport=(?<transport1>tcp|sctp|udp)\b(.*;protocol=(?<protocol1>diameter|radius|tacacs+)\b|)|;protocol=(?<protocol2>diameter|radius|tacacs+)\b(.*;transport=(?<transport2>tcp|sctp|udp)\b|)
注意
使用两个单独的正则表达式和两个调用find
来扫描传输和协议值会更简单。至少正则表达式要简单得多