Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何验证带有冒号限制的字符串_Java_Regex - Fatal编程技术网

Java 如何验证带有冒号限制的字符串

Java 如何验证带有冒号限制的字符串,java,regex,Java,Regex,我正在尝试编写一个布尔函数,以便仅对字符串支持双冒号:。它应该拒绝任何具有非连续冒号或两个以上连续冒号的字符串。双冒号的外观可以是任意数字。我可以编写支持双冒号的正则表达式,但我不知道如何拒绝这么多非连续冒号和连续冒号的组合。任何想法都很感激 有效输入:Customer::Table,Customer::Table::Sub 无效输入:Customer:Table,Customer::Table:Sub,Customer:::Table这里有一行选项使用了String>匹配项: String i

我正在尝试编写一个布尔函数,以便仅对字符串支持双冒号
。它应该拒绝任何具有非连续冒号或两个以上连续冒号的字符串。双冒号的外观可以是任意数字。我可以编写支持双冒号的正则表达式,但我不知道如何拒绝这么多非连续冒号和连续冒号的组合。任何想法都很感激

有效输入:
Customer::Table
Customer::Table::Sub


无效输入:
Customer:Table
Customer::Table:Sub
Customer:::Table

这里有一行选项使用了
String>匹配项

String input = "Customer::Table::Sub";
if (input.matches("[^:]+(?:::[^:]+)*")) {
    System.out.println("MATCH");
}

下面是对所用正则表达式的解释:

[^:]+          match one or more non colon characters
(?:            start non capturing group
    ::[^:]+    match :: again followed by one or more non :
)*             the group occurring zero or more times

您能给出有效和无效输入的示例吗?@thibsc用几个示例更新描述。优雅!我尝试了一些其他的组合,它们都奏效了。你能为
(?::[^::+)
添加一些解释吗?我已经在正则表达式中添加了一个解释。谢谢。我想知道我们是否真的需要非捕获组?似乎它的目的是解析字符串,但我的函数将是布尔函数。@dashenswen您可以删除非捕获组,但保留它可能会稍微提高性能。您能给我一个快速链接,解释为什么它会提高性能吗