簡體   English   中英

具有條件的條件的Groovy范例

[英]Groovy paradigm for conditional with find

此代碼有效,但是重復查找似乎並非最佳選擇。 無需重復就可以實現相同的功能嗎?

def pattern = ~'some_regex'
def inFile = new File('in')

inFile.eachLine { String line ->
    if (line.find(pattern)) {
        line.find(pattern) { match ->
            ... // do something
        }   
    } 
    else {
        ... // do something (else)
    }
}

我建議使用eachMatch()

inFile.eachLine { String line ->
    String matched
    line.eachMatch( pattern ){ 
      matched = it[ 0 ]
      doSomethingWithMatch matched
    }
    if( !matched ) doNoMatch()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM