Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
什么';我的Groovy regex怎么了?_Regex_String_Groovy_Patternsyntaxexception - Fatal编程技术网

什么';我的Groovy regex怎么了?

什么';我的Groovy regex怎么了?,regex,string,groovy,patternsyntaxexception,Regex,String,Groovy,Patternsyntaxexception,这是我的密码: String myRegex = "*cow" String name = "SHIRACOWPEPPER" name = name.toLowerCase() if(!name || name.matches(myRegex)) { return true } 当我运行此命令时,我得到一个PatternSyntaxException:在索引0*cow^附近悬空的元字符“*”错误。想法?这个*是一个元字符,意思是“零次或多次”匹配您之前匹配的内容,但在这种情况下,没有

这是我的密码:

String myRegex = "*cow"
String name = "SHIRACOWPEPPER"
name = name.toLowerCase()

if(!name || name.matches(myRegex)) {
    return true
}

当我运行此命令时,我得到一个
PatternSyntaxException:在索引0*cow^
附近悬空的元字符“*”错误。想法?

这个
*
是一个元字符,意思是“零次或多次”匹配您之前匹配的内容,但在这种情况下,没有任何匹配。这可能会起作用:

String myRegex = ".*cow"
String name = "SHIRACOWPEPPER"
name = name.toLowerCase()

if(!name || name.matches(myRegex)) {
    return true
}

有关更多信息,请参见您可能想说的
字符串myRegex=“.*cow”
,这意味着cow之前的任何数字字符都是后缀。

您也可以这样做
!名称| |名称==~myRegex