ฉันไม่แน่ใจ 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
person
Stefan
schedule
15.02.2020