开发者

groovy regexpression

开发者 https://www.devze.com 2023-01-01 10:57 出处:网络
How to get fi开发者_开发技巧le name from these lines using groovy . File file = new File(SOURCE_FILE_NAME).eachLine{line->

How to get fi开发者_开发技巧le name from these lines using groovy .

File file = new File(SOURCE_FILE_NAME).eachLine{line->
    println line
}

getting line like this :

/usr/local/
/usr/local/testing.groovy
/usr/local/picture.jpg

expecting output:

testing.groovy
picture.jpg

thanks


File file = new File(SOURCE_FILE_NAME).eachLine{ path ->
    println org.apache.commons.lang.StringUtils.substringAfterLast(path, "/")
}


I don't know about groovy, but the regex you're looking for would be[^/]+$


println (new File(yourString).name)

This should work.


also (no external deps)

 ["/usr/local", "/usr/local/testing.groovy", 
  "/usr/local/picture.jpg"].each { item->
    def pat = item =~ /(\w*\/)*(.*)/ 
    if(pat.matches()) {
        println pat.group(2)
    }  
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消