2017-10-12 63 views
0

我如何能够对Groovy中的字符串中的两个不同单词进行regex?确实,我只能在找到一个字符串时才能使用它。在Groovy中寻找两个字符串

def r = "This is a line that only contains LookForMe and nothing else" 
def result = r =~ ('LookForMe' || 'AndMeToo') 
assert result instanceof Matcher 
if (result) ... 

我希望能够找的话“LookForMe”或“AndMeToo”,如果这两种情况下是“真正”执行的操作。

+0

而你想匹配正则表达式的字符串在哪里? – alfasin

+0

定义为r。这并不是说这个例子真的很重要。问题是关于如何匹配单个变量的两个字符串。 – user8755872

+0

用'|'替代'||' – alfasin

回答

1
def r = "This is a line that only contains LookForMe and nothing else" 
def result = (r =~ /.*LookForMe.*|.*AndMeToo.*/) 
if(result) ...