-1

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

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

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

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);
        }

else if inputtext match pattern2 然后执行{语句},

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);

else if inputtext match pattern3 然后执行{语句},

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");
    }   

请帮我写这个鳕鱼

4

2 回答 2

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(它们都是字符串)进行匹配——当然,您永远不会得到匹配,您重新比较苹果和苹果派。相反,您应该为每个所需模式创建一个匹配器,然后按顺序检查每个模式。您会注意到我将所有相同的打印件移到了它们自己的块中 - 现在可以一次全部更新。

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

编辑:在我看来,这可以通过更多的编程方式完成。在同一个类的某处声明一个模式包装器:

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);
}

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

于 2013-03-19T09:07:31.877 回答
0

尝试使用

if(p.equals(p1)) 

代替

if(p==p1)

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

于 2013-03-19T08:50:50.980 回答