Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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,我有一个包含行的字典文件 ns*.abc.com ns*.xyz.com 我想将诸如ns15.abc.com、nsABC.abc.com之类的模式与字典文件匹配,并返回true。 例如,ns15.abc.com是匹配的,而ns16.abc.abc.com不是匹配的。 提前谢谢 public class ValidateDemo { public static void main(String[] args) { List<String> input = new

我有一个包含行的字典文件
ns*.abc.com
ns*.xyz.com
我想将诸如ns15.abc.com、nsABC.abc.com之类的模式与字典文件匹配,并返回true。 例如,ns15.abc.com是匹配的,而ns16.abc.abc.com不是匹配的。 提前谢谢

public class ValidateDemo {  
    public static void main(String[] args) {  
    List<String> input = new ArrayList<String>();  

    input.add("ns14.abc.com");
    for (String str : input) {
        if (str.matches("ns*.abc.com")) {
            System.out.println("Match: " + str);
        }
    }
}
}
public类ValidateDemo{
公共静态void main(字符串[]args){
列表输入=新的ArrayList();
input.add(“ns14.abc.com”);
for(字符串str:input){
如果(str.matches(“ns*.abc.com”)){
System.out.println(“匹配:+str”);
}
}
}
}

在字典中的每个条目中,将星号替换为“[^\.]*”并将点替换为“
”\.
”-每个条目都有自己的模式:

    Pattern p = Pattern.compile("^" + dictEntry.replaceAll(".", "\\.").replaceAll("*", "[^\\.]*") + "$");

或者,您也可以将所有字典条目与“|”连接起来,使单个模式与所有条目匹配。

您可以向我们展示您已经尝试过的内容吗?它将帮助我们知道您正在尝试做什么。public类ValidateDemo{public static void main(String[]args){List input=new ArrayList();input.add(“ns14.abc.com”);for(String str:input){if(str.matches(“ns*.abc.com”){System.out.println(“Match:+str”);}}请不要在评论中发布它。把它编辑成你的问题。对不起,这是偶然的。我已经更新了帖子。你的字符串没有使用正则表达式语法。注意:如果您使用Java 6,而不是转义点,您可以用
\Q
..
\E
@fge包围文本,
\E
@fge,或者更一般地说,使用
模式。quote
,它正是为这项任务而设计的,可以将字符串转换为与该字符串完全匹配的正则表达式。