java - 使用正则表达式获取文件名

标签 java string

我的文件夹中有很多 .json 文件。我必须获得一个全名“ranjans-vra2-standalone-269d9199a-0.vraCafe.0-nimbus-vra-deploy-result.json”的文件。同样,我有很多文件夹,我只想从这些文件夹中获取这个文件。为此,我想使用正则表达式。

每个文件的共同点是:“vraCafe”和“.json”。

我必须使用它作为 JSONObject 的 testbedPath abd 供应。

JSONObject jsonObject = readSimpleJson(testbedPath);

我应该使用什么正则表达式来获取 testbedPath?

最佳答案

如果你想使用正则表达式,你可以使用:

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

它将每个文件名与文件扩展名 .json 相匹配,并且 名称中的 vraCafe

例如:

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

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");
        }
    }
}

关于java - 使用正则表达式获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645015/

相关文章:

java - 哈希码和等于

java - org.springframework.test 也无法在存在 Maven 依赖项的情况下解析

java - 如何在雪豹上安装 java jdk 7

java - IntelliJ IDEA : support both SBT and Maven on a single project

java - Equinox 启动器可执行文件

程序中找不到打印最长输入行的bug

c++ - 除非已经转义,否则如何替换所有出现的字符串或字符?

swift - 对 Swift 中枚举的误解

javascript - 在javascript中使用变量的值作为变量

c++ - 如何检查字符串是否在字符串数组中