Pattern Matching in Strings in Groovy – Groovy中字符串的模式匹配

最后修改: 2019年 4月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll look at the Groovy language features for pattern matching in Strings.

在这篇文章中,我们将看看Groovy语言在字符串中模式匹配的特点。

We’ll see how Groovy’s batteries-included approach provides us with a powerful and ergonomic syntax for our basic pattern matching needs.

我们将看到Groovy的包含电池的方法是如何为我们的基本模式匹配需求提供一个强大的、符合人体工程学的语法。

2. Pattern Operator

2.模式操作员

The Groovy language introduces the so-called pattern operator ~. This operator can be considered a syntactic sugar shortcut to Java’s java.util.regex.Pattern.compile(string) method.

Groovy语言引入了所谓的模式操作符~。这个操作符可以说是Java的java.util.regex.Pattern.compile(string)方法的语法糖捷径。

Let’s check it out in practice as a part of a Spock test:

让我们在实践中检查一下,作为Spock测试的一部分。

def "pattern operator example"() {
    given: "a pattern"
    def p = ~'foo'

    expect:
    p instanceof Pattern

    and: "you can use slashy strings to avoid escaping of blackslash"
    def digitPattern = ~/\d*/
    digitPattern.matcher('4711').matches()
}

This is also pretty convenient, but we’ll see that this operator is merely the baseline for some other, even more useful operators.

这也是相当方便的,但我们会看到这个操作符只是其他一些更有用的操作符的基线。

3. Match Operator

3. 匹配操作员

Most of the time, and especially when writing tests, we’re not really interested in creating Pattern objects, but instead, want to check if a String matches a certain regular expression (or Pattern). Groovy, therefore, also contains the match operator ==~.

大多数时候,尤其是在编写测试时,我们对创建Pattern对象并不感兴趣,而是想检查一个String是否与某个正则表达式(或Pattern)匹配。因此,Groovy也包含匹配运算符==~

It returns a boolean and performs a strict match against the specified regular expression. Basically, it’s a syntactic shortcut over calling Pattern.matches(regex, string).

它返回一个boolean并对指定的正则表达式进行严格匹配。基本上,它是调用Pattern.matches(regex, string)的一个语法快捷方式。

Again, we’ll look into it in practice as part of a Spock test:

同样,我们将在实践中研究它作为Spock测试的一部分。

def "match operator example"() {
    expect:
    'foobar' ==~ /.*oba.*/

    and: "matching is strict"
    !('foobar' ==~ /foo/)
}

4. Find Operator

4.查找操作员

The last Groovy operator in the context of pattern matching is the find operator ~=. In this case, the operator will directly create and return a java.util.regex.Matcher instance.

在模式匹配方面,最后一个Groovy操作符是查找操作符~=。在这种情况下,该操作符将直接创建并返回一个java.util.regex.Matcher实例。

We can act upon this Matcher instance, of course, by accessing its known Java API methods. But in addition, we’re also able to access matched groups using a multi-dimensional array.

当然,我们可以通过访问其已知的Java API方法对这个Matcher实例采取行动。但除此之外,我们还可以使用一个多维数组访问匹配的组。

And that’s not all — the Matcher instance will automatically coerce to a boolean type by calling its find() method if used as a predicate. Quoting the official Groovy docs, this means “the =~ operator is consistent with the simple use of Perl’s =~ operator”.

这还不是全部–如果被用作谓词,Matcher实例将通过调用其find()方法自动强制为boolean类型。引用Groovy官方文档,这意味着”=~操作符与Perl的=~操作符的简单使用一致”。

Here, we see the operator in action:

在这里,我们看到操作员在行动。

def "find operator example"() {
    when: "using the find operator"
    def matcher = 'foo and bar, baz and buz' =~ /(\w+) and (\w+)/

    then: "will find groups"
    matcher.size() == 2

    and: "can access groups using array"
    matcher[0][0] == 'foo and bar'
    matcher[1][2] == 'buz'

    and: "you can use it as a predicate"
    'foobarbaz' =~ /bar/
}

5. Conclusion

5.总结

We’ve seen how the Groovy language gives us access to the built-in Java features regarding regular expressions in a very convenient manner.

我们已经看到Groovy语言是如何以一种非常方便的方式让我们访问有关正则表达式的内置Java功能的。

The official Groovy documentation also contains some concise examples regarding this topic. It’s especially cool if you consider that the code examples in the docs are executed as part of the documentation build.

官方的Groovy文档也包含了一些关于这个主题的简洁的例子。如果你考虑到文档中的代码示例是作为文档构建的一部分来执行的,那就特别酷。

As always, code examples can be found on GitHub.

一如既往,代码实例可以在GitHub上找到。