添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

groovy regex find all matches

Groovy中,要使用正则表达式匹配并查找所有匹配项,可以使用 findAll 方法。

findAll 方法会在字符串中查找所有匹配的子字符串,并返回一个匹配列表,列表中包含了所有匹配项。该方法接收一个正则表达式作为参数。

以下是一个示例代码,用于演示如何使用 findAll 方法查找所有匹配项:

def input = "Groovy is great. Groovy is powerful."
def pattern = /Groovy/
def matches = input.findAll(pattern)
matches.each { match ->
    println "Match found: $match"

上面的代码会输出以下结果:

Match found: Groovy
Match found: Groovy

在这个例子中,我们定义了一个字符串变量input,其中包含了两个"Gorovy"字符串。然后,我们使用正则表达式/Groovy/作为参数调用findAll方法。最后,我们遍历返回的匹配列表,并打印出每个匹配项。

注意,findAll方法返回的是一个Matcher对象的迭代器,而不是一个列表。因此,我们可以使用each方法遍历所有匹配项。

希望这个例子可以帮助你了解如何在Groovy中使用正则表达式查找所有匹配项。

  •