Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更好的正则表达式匹配字符串?_Java_Regex - Fatal编程技术网

Java 更好的正则表达式匹配字符串?

Java 更好的正则表达式匹配字符串?,java,regex,Java,Regex,我有一个“moreinfo”目录,其中有一些html文件和其他文件夹。我正在moreinfo目录(而不是moreinfo中的子目录)中搜索与toolId*匹配的文件。该文件的名称与toolId]相同 下面是我如何编写它的代码片段,如果我的toolId=delegatedAccess列表返回2个文件(delegatedAccess.html&delegatedAccess.shopping.html),该文件基于宽卡片过滤器(toolId*) 它们是否是编写正则表达式的更好方法,该正则表达式将检

我有一个“
moreinfo
”目录,其中有一些html文件和其他文件夹。我正在
moreinfo
目录(而不是
moreinfo
中的子目录)中搜索与
toolId*
匹配的文件。该文件的名称与toolId]相同

下面是我如何编写它的代码片段,如果我的
toolId=delegatedAccess
列表返回2个文件(
delegatedAccess.html&delegatedAccess.shopping.html
),该文件基于宽卡片过滤器(
toolId*

它们是否是编写正则表达式的更好方法,该正则表达式将检查到最后一个出现的时段,并返回与我的toolId完全匹配的文件

infoDir =/Users/moreinfo


 private String getMoreInfoUrl(File infoDir, String toolId) {
        String moreInfoUrl = null;
        try {
            Collection<File> files = FileUtils.listFiles(infoDir, new WildcardFileFilter(toolId+"*"), null);
            if (files.isEmpty()==false) {
            File mFile = files.iterator().next();
            moreInfoUrl = libraryPath + mFile.getName(); // toolId;
        }
    } catch (Exception e) {
        M_log.info("unable to read moreinfo" + e.getMessage());
    }
    return moreInfoUrl;

}
infoDir=/Users/moreinfo
私有字符串getMoreInfoUrl(文件infoDir,字符串工具ID){
字符串moreInfoUrl=null;
试一试{
Collection files=FileUtils.listFiles(infoDir,新的通配符filefilter(toolId+“*”),null);
if(files.isEmpty()==false){
File mFile=files.iterator().next();
moreInfoUrl=libraryPath+mFile.getName();//工具ID;
}
}捕获(例外e){
M_log.info(“无法读取更多信息”+e.getMessage());
}
返回moreInfoUrl;
}

这就是我在所有精彩评论中所做的。我做了字符串操作来解决我的问题。因为Regex不是正确的解决方案

private String getMoreInfoUrl(File infoDir, String toolId) {
    String moreInfoUrl = null;
    try { 
        Collection<File> files = FileUtils.listFiles(infoDir, new WildcardFileFilter(toolId+"*"), null);
        if (files.isEmpty()==false) {
                for (File mFile : files) {
                    int lastIndexOfPeriod = mFile.getName().lastIndexOf('.');
                    String fNameWithOutExtension = mFile.getName().substring(0,lastIndexOfPeriod);
                    if(fNameWithOutExtension.equals(toolId)) {
                        moreInfoUrl = libraryPath + mFile.getName();
                        break;
                    }

                }
        }
    } catch (Exception e) {
        M_log.info("unable to read moreinfo" + e.getMessage());
    }
    return moreInfoUrl;

}
私有字符串getMoreInfoUrl(文件infoDir,字符串工具ID){
字符串moreInfoUrl=null;
试试{
Collection files=FileUtils.listFiles(infoDir,新的通配符filefilter(toolId+“*”),null);
if(files.isEmpty()==false){
对于(文件mFile:files){
int lastIndexOfPeriod=mFile.getName().lastIndexOf('.');
字符串fNameWithOutExtension=mFile.getName().substring(0,lastIndexOfPeriod);
if(fNameWithOutExtension.equals(toolId)){
moreInfoUrl=libraryPath+mFile.getName();
打破
}
}
}
}捕获(例外e){
M_log.info(“无法读取更多信息”+e.getMessage());
}
返回moreInfoUrl;
}

那么在您的示例中,您希望它只返回
delegatedAccess.html
文件吗?请尝试
toolID+“\\.\.[^\.]*$”
@clcto:yes。现在让我试试你的,我的例子是regex,但是
WildcardFileFilter
不使用regex。我不认为有任何方法可以指定使用
通配符文件筛选器所需的内容,但您可以迭代它返回的列表。@clcto:是的,您是正确的。WildCardFileFilter不使用正则表达式。假设您有一个非常小的有限的允许文件扩展名集,安全吗?在本例中,您可以编写正则表达式来检查
toolId.
,类似于
Pattern.compile(“^(“+toolId+”\\。(?:html?;aspx?;jsp?)$”
。您的
if(files.isEmpty()==false)
可以一起删除。
for
循环将仅对列表中的元素进行迭代。