==〜布尔运算符是做什么的?

时间:2015-09-14 12:38:40

标签: grails groovy operators

这个==~布尔运算符在下面的代码中做了什么?

springcacheCacheManager.cacheNames.each { name ->
    if (cacheNamePatterns.any { name ==~ it }) {
        flushNamedCache(name)
    }
}

1 个答案:

答案 0 :(得分:2)

match operator执行正则表达式匹配:

def regex = "[A-Z]{2,}"

assert "wot" !=~ regex
assert "A" !=~ regex
assert "ABC" ==~ regex

在您的代码中,如果cacheName与任何cacheNamePatterns匹配,则会刷新它:

def cacheNames = ['html', 'res/images', 'res/js']

def cacheNamePatterns = ['res/.*']

def flushedCaches = []


cacheNames.each { name ->
    if (cacheNamePatterns.any { name ==~ it }) {
        flushedCaches << name
    }
}

assert flushedCaches == ['res/images', 'res/js']