gpt4 book ai didi

java - 当 Java 代码中存在两个组时,使用 OR 验证正则表达式组

转载 作者:行者123 更新时间:2023-12-01 18:25:31 25 4
gpt4 key购买 nike

需要验证传输和协议(protocol)之一可以存在或两者都可以存在的格式。我已应用此正则表达式来验证这两个组。但是,当任何一个验证成功时,它不会验证其他组,就像代码中一样。注意:传输可以接受 tcp、sctp 或 udp,协议(protocol)可以接受半径、直径和 tacacs+。

例如,如果传输和协议(protocol)都是 URL 的一部分,并且传输格式正确并且使用正则表达式中提供的值进行验证,那么即使协议(protocol)的值无效,它也会返回 true。

Java 代码:

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,因为它成功验证了传输,但无法验证协议(protocol)值。

最佳答案

我不是 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

我替换了?通过.+因为?仅匹配单个字符,.+匹配多个字符。然后你可以使用match()而不是find()

match() 要求整个字符串与模式匹配。 find() 要求只有字符串的一部分与模式的一部分匹配。我认为您想要匹配(未找到)。

我替换了.*通过.+因为.*匹配任意数量的字符(包括零个),但我假设您只想在此处匹配至少一个字符。

我删除了\\b因为它匹配字符 b从字面上看,这似乎不是您想要的。

我替换了tacacs+通过tacacs\\+因为你想匹配 +从字面上看,不匹配所有以 tacacs 开头的单词以及任意数量的s喜欢 tacacsssssss .

网站https://regex101.com/对于测试正则表达式非常有帮助,因为它用颜色标记了模式的各个部分并解释了这些部分的含义。

不要使用您不理解的复杂表达式,您可以单独检查这两个部分,然后编写 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
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

关于java - 当 Java 代码中存在两个组时,使用 OR 验证正则表达式组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com