测试字符串是
It is a test data
%Warning: portfast should only be enabled on ports connected to a single
host. Connecting hubs, concentrators, switches, bridges, etc. to this
interface when portfast is enabled can cause temporary bridging loops.
Use with CAUTION
Test is a test.
如果字符串中包含%
符号,则该字符串无效。但如果%
后面跟着以下字符序列,则有效,“%警告:只应在连接到单个主机的端口上启用portfast。连接集线器,集中器,交换机,网桥等。当启用portfast时,此接口可能会导致临时桥接循环。\ n请小心使用“。即使在这种情况下,'%'符号也只能出现一次。
我的代码是:
public class Abc {
public static void main(String[] args) {
String reg="%(Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTION)([^%])";
String str = "%Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTIONTest is a testData is invalid%";
Pattern regEx2 = Pattern
.compile(reg);
Matcher matcher = regEx2.matcher(str);
if (matcher.find()) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
}
}
但我没有得到预期的结果。 我可以得到合适的正则表达式吗?
答案 0 :(得分:1)
也许你可以将你的代码与另一个得到'%'
的表达式合并 public static void main(String[] args) {
String reg = "%Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTION";
String str = "%Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTIONTest is a testData is invalid%";
Pattern regEx2 = Pattern.compile(reg);
Matcher matcher = regEx2.matcher(str);
int occurences = str.length() - str.replace("%", "").length();
if (matcher.find() && occurences <= 1) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
}
如果你有StringUtils类,那么你可以使用它的方法 countMatches
答案 1 :(得分:0)
好的,您的代码中存在两个问题:
find
方法仅尝试在字符串中查找给定模式的出现。在您的情况下,您需要使用matches
,它检查完整字符串是否与模式匹配。([^%])
- 这只匹配一个不是%
的字符。要匹配任意数量的字符,请使用([^%]*)
。 *
表示&#34; 0或更多&#34;,或者您可能需要+
,而这意味着&#34; 1或更多&#34;。结果代码如下所示:https://ideone.com/Q2y21b
public static void main(String[] args) {
String reg="%(Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTION)([^%]+)";
String str = "%Warning: portfast should only be enabled on ports connected to a single\n host. Connecting hubs, concentrators, switches, bridges, etc. to this\n interface when portfast is enabled can cause temporary bridging loops.\n Use with CAUTIONTest is a testData is invalid%";
Pattern regEx2 = Pattern
.compile(reg);
Matcher matcher = regEx2.matcher(str);
if (matcher.matches()) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
}