Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_String_Pattern Matching - Fatal编程技术网

带星号的java正则表达式匹配字符串

带星号的java正则表达式匹配字符串,java,regex,string,pattern-matching,Java,Regex,String,Pattern Matching,我必须创建一个正则表达式来比较插入以下列表中的字符串: ArrayList<String> entries = new ArrayList<>(); entries.add("text1/*"); entries.add("text2/*/*"); entries.add("text2/*/*/*"); entries.add("text1/*/*"); entries.add("text1/value1/*"); entries.add("*/*"); ArrayLi

我必须创建一个正则表达式来比较插入以下列表中的字符串:

ArrayList<String> entries = new ArrayList<>();
entries.add("text1/*");
entries.add("text2/*/*");
entries.add("text2/*/*/*");
entries.add("text1/*/*");
entries.add("text1/value1/*");
entries.add("*/*");
ArrayList entries=new ArrayList();
条目。添加(“text1/*”;
条目。添加(“text2/*/*”;
条目。添加(“text2/*/*/*”;
条目。添加(“text1/*/*”;
条目。添加(“text1/value1/*”;
条目。添加(“*/*”);

如果我有一个像“text1/value1/value2”这样的字符串,我怎么能理解哪个是遍历条目的匹配字符串?例如,在上面的条目中,正确的是第五个条目“text1/value1/*”

我不知道如何为此编写正则表达式,只需逐个字符地解析它似乎要容易得多

然而,我刚刚做了一个使用Spring框架提供的路径匹配器的项目,它看起来是一个完美的工作

publicstaticvoidmain(字符串[]args)引发异常{
org.springframework.util.AntPathMatcher pathMatcher=new org.springframework.util.AntPathMatcher();
字符串路径=“text1/value1/value2”;
ArrayList条目=新的ArrayList();
条目。添加(“text1/*”;
条目。添加(“text2/*/*”;
条目。添加(“text2/*/*/*”;
条目。添加(“text1/*/*”;
条目。添加(“text1/value1/*”;
条目。添加(“*/*”);
字符串bestMatch=null;
//使用模式比较器查看与此路径最匹配的模式
Comparator comp=pathMatcher.getPatternComparator(路径);
//检查我们所有的模式
用于(字符串模式:条目){
//确保模式与路径匹配
if(pathMatcher.match(模式、路径)){
//检查我们是否已经有了匹配项
如果(最佳匹配!=null){
//如果我们有一场比赛,那就选这两场比赛中最好的一场,最好的一场
如果(成分比较(最佳匹配,模式)>0){
最佳匹配=模式;
}
}否则{
//尚未找到匹配项,让我们使用此项
最佳匹配=模式;
}
}
}
系统输出打印项次(最佳匹配);
}

EDIT1:找到更好的正则表达式。您可以将类似的逻辑应用于您的需求。我猜您必须基于输入字符串构建正则表达式

对所用模式的解释:

^([^/*]+)/([^/*]+)/[*]$ ^ start of string [^/*] match any character except / or * + is a quantifier that matches preceding char or group 0 to many times / followed by the character / [^/*] match any character except / or * / followed by the character / [*] followed by only 1 more character which is * and not something else $ end of string ^([^/*]+)/([^/*]+)/[*]$ ^字符串开头 [^/*]匹配除/或以外的任何字符* +是与前面的字符或组0匹配多次的量词 /其次是人物/ [^/*]匹配除/或以外的任何字符* /其次是人物/ [*]后面只跟1个字符,是*而不是其他字符 $字符串结尾
publicstaticvoidmain(字符串[]args){
最终ArrayList条目=新ArrayList();
条目。添加(“text1/*”;
条目。添加(“text2/*/*”;
条目。添加(“text2/*/*/*”;
条目。添加(“text1/*/*”;
条目。添加(“text1/value1/*”;
条目。添加(“*/*”);
最终模式=模式。编译(“^([^/*]+)/([^/*]+)/[*]$”,2);
for(字符串条目:条目){
Matcher Matcher=pattern.Matcher(条目);
while(matcher.find()){
System.out.println(“找到正确的条目:“+条目”);
}
}
}

那么您要寻找的正确模式是什么?是的是“text1/value1/*”第五个我用更好的正则表达式编辑了我的答案。试一试 ^([^/*]+)/([^/*]+)/[*]$ ^ start of string [^/*] match any character except / or * + is a quantifier that matches preceding char or group 0 to many times / followed by the character / [^/*] match any character except / or * / followed by the character / [*] followed by only 1 more character which is * and not something else $ end of string
public static void main(String[] args) {

    final ArrayList<String> entries = new ArrayList<String>();
    entries.add("text1/*");
    entries.add("text2/*/*");
    entries.add("text2/*/*/*");
    entries.add("text1/*/*");
    entries.add("text1/value1/*");
    entries.add("*/*");

    final Pattern pattern = Pattern.compile("^([^/*]+)/([^/*]+)/[*]$", 2);

    for (String entry : entries) {
        Matcher matcher = pattern.matcher(entry);
        while (matcher.find()) {
            System.out.println("found correct entry: " + entry);
        }
    }
}