2017-10-12 50 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) ...