Bnaffy
Bnaffy

Reputation: 129

Find files that match the regex pattern

I need to find all files that match a particular pattern in grails.

The files will be labeled as "runid.started.xml". So I'm looking to find all using the following regex:

/(? <=\.)(.*?)(?=\.)/

I can find all files but I need to likit it to files the match the pattern. I found a few examples but none seem to work. This is the latest:

New File (c:\\mydirectory\\test ).eachFileRecurse (Files)
{
  if (it.name ==~ /(? <=\.)(.*?)(?=\.)/){
    println it
   {
      println "nope"
    }

This returns "nope"... I'm very new to grails so I'm not sure where I'm going wrong. My regex seems correct in an online regex tester but I may be wrong.

Upvotes: 0

Views: 1070

Answers (1)

Jonathan Mee
Jonathan Mee

Reputation: 38919

==~ is a match operator. Meaning the string in question must be an exact match. And (? <=\.)(.*?)(?=\.) doesn't match "runid.started.xml". So you have two options:

  1. Use the search operator in your if statement: =~
  2. Write a regex that exactly matches all the file names something like: \w*\.(\w+)\.\w*

Upvotes: 1

Related Questions