-1

(ユーザーからの) 入力テキストをパターンと一致させたい。

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

if inputtext match pattern1 Then execute {the 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);
        }

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

これで、追加したいパターンがいくつあっても、それらを 1 か所に追加するだけで済みます。コード内の 1 行を移動するだけで、パターンがチェックされる順序を簡単に制御することもできます。

于 2013-03-19T09:07:31.877 に答える
0

使ってみてください

if(p.equals(p1)) 

それ以外の

if(p==p1)

文字列を比較しようとしているからです。

于 2013-03-19T08:50:50.980 に答える