Shashi Ranjan
Shashi Ranjan

Reputation: 1561

Get filename with regex

I have many .json file in a folder. And I have to get a file with full name "ranjans-vra2-standalone-269d9199a-0.vraCafe.0-nimbus-vra-deploy-result.json". Like wise I have so many folders and I want to get only this file out of those folders. For that I want to use regex.

The common thing for each file is: "vraCafe" and ".json".

I have to use it as testbedPath abd supply to JSONObject.

JSONObject jsonObject = readSimpleJson(testbedPath);

What regex sholud I use to get testbedPath?

Upvotes: 0

Views: 932

Answers (2)

Zelldon
Zelldon

Reputation: 5516

If you want to use a regex you can use:

([\w\S]*(vraCafe)[\w\S]*(\.json)$)

It will match each file name with the fileextension .json and vraCafe in the name.

For Example:

ranjansvra-vra2-standalone-ve269d9199a-0.0-nimbus-vra-vraCafedeploy-result.json

or

ranjansvra-vra2-svraCafetandalone-ve269d9199a-0.0-nimbus-vra-deploy-result.json

See the running example:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String filename = "ranjansvra-vra2-svraCafetandalone-ve269d9199a-0.0-nimbus-vra-deploy-result.json";

        Pattern p = Pattern.compile( "([\\w\\S]*(vraCafe)[\\w\\S]*(\\.json)$)");
        Matcher m = p.matcher(filename);
        if (m.matches()) {
            System.out.println("Matches");
        }
    }
}

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

Just find if the filename contains 'vraCafe' and '.json', if yes, delete it.

 if(fileName.contains("vraCafe") && fileName.contains(".json")){
    //get it and delete it
  }

Edit: In order to have a concrete check on file extension

 if(fileName.contains("vraCafe") && fileName.endsWith(".json")){
    //get it and delete it
  }

Upvotes: 0

Related Questions