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

Java正则表达式

Java正则表达式,java,regex,match,Java,Regex,Match,我正在使用Java1.7,希望编写一些代码来检测另一个字符串中是否存在字符串。 传入字符串将是以下2个值之一: -“安装内部版本” -“安装我的版本” 我需要检查字符串中出现的2个可能值。我知道我可以使用if-else,但我认为模式匹配会更好,因为将来可能还会有新的值 我的代码是: String pattern = "(Install)(.*)(build)"; String option = "Install build"; //see if this matches

我正在使用Java1.7,希望编写一些代码来检测另一个字符串中是否存在字符串。 传入字符串将是以下2个值之一: -“安装内部版本” -“安装我的版本”

我需要检查字符串中出现的2个可能值。我知道我可以使用if-else,但我认为模式匹配会更好,因为将来可能还会有新的值

我的代码是:

    String pattern = "(Install)(.*)(build)";
    String option = "Install build"; //see if this matches

    Pattern r = Pattern.compile(pattern);        
    Matcher matcher = r.matcher(option);

    if (matcher.find()) {
        System.out.println("Match found");
    } else {
        System.out.println("Match not found");
    }

任何帮助都会很好。

Regexp是处理具有未知格式的字符串(即计数特定模式的出现次数,或捕获字符串的未知子部分等)的强大工具

在您的情况下,使用Regexp没有多大意义,因为您已经知道每个字符串的可能性。

这似乎有效:

Install(?: my)? build

“也许要去?”梅罗斯,请读一篇文章。在@holroy:OP发布之前,代码应该是有效的。他只是想除掉其他人。