模式与文件路径输入不匹配

时间:2018-08-09 08:21:04

标签: java regex

我的else语句中有问题。它显示“未找到模式” ,因为该值与String相匹配。有人可以帮我弄这个吗?

以下是代码:

public static void main(String[] args) throws JSONException {
        String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/_apis/tfvc/items?scopePath=$/IRIS.Base.5.0.1/CI_CD/Install/ReleaseNotes/&recursionLevel=OneLevelPlusNestedEmptyFolders&api-version=2.1";
        String response = getURLResponse(url);
        JSONObject obj_JSONObject = new JSONObject(response.toString());
        JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");

        for (int i = 0; i < obj_JSONArray.length(); i++) {
            JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);
            String value = obj_JSONObject2.getString("path");
            String Release_Pattern_Value1 = "5.0.1.78";

            String input = value;
            Matcher matcher = Pattern.compile("ReleaseNote.*[0-9]*.[0-9]*.[0-9]*.[0-9]").matcher(input);
            if (matcher.find()) {
                String pattern = matcher.group(0);
                String pattern_value = pattern.split("_")[1];

                if (pattern_value.equals(Release_Pattern_Value1)) {
                    System.out.println(pattern_value);
                    System.out.println("Pattern Match");
                    break;

                }
            } else {
                System.out.println("Pattern Not Found");
            }
        }
}
在以下语句中设置为value变量的 path 键的

JSON值

String value = obj_JSONObject2.getString("path");

评估为 $ / IRIS.Base.5.0.1 / CI_CD / Install / ReleaseNotes / ReleaseNotes_5.0.1.73.txt

1 个答案:

答案 0 :(得分:0)

在与正则表达式匹配之前,您应该忽略路径扩展名

String releaseNote=value.substring(value.lastIndexOf("/")+1);//remove the path
releaseNote=releaseNote.substring(0,releaseNote.lastIndexOf('.'));//remove the extension
Matcher matcher = Pattern.compile("^ReleaseNotes_.*[0-9]*.[0-9]*.[0-9]*.[0-9]$")
         .matcher(releaseNote);//add '^' '$' in start and end to ensure it matches the begining and end of line