如何通过在java中使用If语句来匹配更多模式与输入文本?

时间:2013-03-19 08:47:19

标签: java regex

我想用模式制作匹配输入文本(来自用户)。

String inputtext =  "book 'learning java' for doctor  ahmed mohamed";

如果inputtext匹配pattern1则执行{statement},

if(p==p1){

        Pattern p = Pattern.compile("(book|\\)|\\:) (.*) for( doctor| author|) (.*)");
        Matcher m = p.matcher(inputtext);
        if (m.find()) {
            String title = m.group(2).trim();
            String author = m.group(4).trim();
            System.out.println("Title is : " + title);
            System.out.println("Author is : " + author);
        }

否则如果inputtext匹配pattern2则执行{the statement},

else if(p==p2){
            Pattern p = Pattern.compile("(.*) (book for) (.*)");
            Matcher m = p.matcher(inputtext);
            if (m.find()) {
                String title = m.group(1).trim();
                String author = m.group(3).trim();
                System.out.println("Title is : " + title);
                System.out.println("Author is : " + author);

否则如果inputtext匹配pattern3则执行{statement},

else if(p==p3){ 
                Pattern p = Pattern.compile("(doctor| author|) (.*) (writ) (.*)");
                Matcher m = p.matcher(inputtext);
                if (m.find()) {
                    String author = m.group(2).trim();
                    String title = m.group(4).trim();
                    System.out.println("Author is : " + author);
                    System.out.println("Title is : " + title);
                }

否则不匹配。

else
    {
        System.out.println("Not match");
    }   

请帮我写这个鳕鱼

2 个答案:

答案 0 :(得分:1)

我认为你真正想做的事情更像是这样:

String inputtext =  "some Input text";

//Initialize your patterns:
String p1 = "pattern one";
String p2 = "pattern two";
String p3 = "pattern three";

//Then, create matchers for each of the patterns on the input text.
Matcher m1=Pattern.compile(p1).matcher(inputtext);
Matcher m2=Pattern.compile(p2).matcher(inputtext);
Matcher p3=Pattern.compile(p3).matcher(inputtext);

//Don't repeat yourself - initialize your author and title out here, then only
//write the print statements once.

String author=null;
String title = null;

if (m1.find()) {                //if inputtext matches p1
    title = m1.group(2).trim();
    author = m1.group(4).trim();
} else if (m2.find()) {           //else if inputtext matches p2
    title = m2.group(1).trim();
    author = m2.group(3).trim();
} else if (m3.find()) {            //else if inputtext matches p3
    author = m3.group(2).trim();
    title = m3.group(4).trim();
}

if (author ==null || title == null) {   //If no matches set the author and title strings...
    System.out.println("Not match");    //There's no match
} else {                                //Otherwise...
                                       // We have a match!
    System.out.println("اسم المؤلف : " + author);
    System.out.println("عنوان الكتاب : " + title);
}

现在,无论出于何种原因,你都试图将p(这是你正在编译的模式)与p1,p2和p3(都是字符串)相匹配 - 当然你永远不会得到一个匹配,你要比较苹果和苹果派。相反,您应该为每个所需的模式创建一个匹配器,然后按顺序检查每个模式。您会注意到我将所有相同的打印件移到了他们自己的块中 - 它现在可以一次更新。

我甚至没有尝试调试你的正则表达式,因为那个脚本会让我的眼睛受伤,我认为这是一个更紧迫的问题。如果它仍然不起作用,那么我们可以进入你的特定正则表达式,但这至少会解决我认为你的问题实际上是什么。

编辑:我发现这可以通过编程方式进行。同一个类中的某个地方声明了一个模式Wrapper:

private class PatternWrapper {
    Pattern pattern;
    int authorGroup;
    int titleGroup;

    public PatternWrapper(String pattern, int authorGroup, int titleGroup) {
        this.pattern = Pattern.compile(pattern);
        this.authorGroup = authorGroup;
        this.titleGroup = titleGroup;
    }
}

然后,这样使用它:

String inputtext =  "some Input text";

//Initialize your patterns:
PatterWrapper[] patterns = {
    new PatternWrapper("pattern one", 4, 2)
    , new PatternWrapper("pattern two", 3, 1)
    , new PatternWrapper("pattern three", 4, 2)
}

//Don't repeat yourself - initialize your author and title out here, then only
//write the print statements once.

String author=null;
String title = null;

for (PatternWrapper pw : patterns){
    matcher = pw.pattern.matcher(inputtext);
    if (matcher.find()) {
        title = matcher.group(pw.titleGroup).trim();
        author = matcher.group(pw.authorGroup).trim();
        break;
    }
}

if (author ==null || title == null) {   //If no matches set the author and title strings...
    System.out.println("Not match");    //There's no match
} else {                                //Otherwise...
                                       // We have a match!
    System.out.println("اسم المؤلف : " + author);
    System.out.println("عنوان الكتاب : " + title);
}

现在,无论您想要添加多少个模式,您只需将它们添加到一个位置即可。您还可以通过仅在代码中移动一行来轻松控制检查模式的顺序。

答案 1 :(得分:0)

尝试使用

if(p.equals(p1)) 

而不是

if(p==p1)

因为您正在尝试比较字符串。