Reputation: 1561
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
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
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
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