-1

I want to make match input text (from user) with pattern.

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 Then execute {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);

else if inputtext match pattern3 Then execute {the 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 not match .

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

please help me to write this cod

  • First of all, use equals http://stackoverflow.com/questions/767372/java-string-equals-versus – BobTheBuilder Mar 19 '13 at 08:49
  • Please provide code containing text in English language. Don't expect others to be able to understand region specific languages. I am not get the input text on which you are applying regex pattern. – Ankur Shanbhag Mar 19 '13 at 08:52
  • Shimaa, did either post below help you? Some of us take a personal interest in making sure that we've helped - it'd be nice for you to provide some feed back. Comment on the answers, or accept an answer if it worked, it's what makes the world go round... – FrankieTheKneeMan Mar 19 '13 at 09:51

2 Answers2

1

I think what you actually want to do is something more like this:

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

Right now, for whatever reason, you're trying to match p (which is a Pattern you're compiling) against p1, p2, and p3 (which are all Strings) - of course you'll never get a match, you're comparing apples and apple pie. Instead, you should create a matcher for each of your desired patterns, then check each of them in sequence. You'll noticed I moved all those Identical prints out into their own block - it can now be updated all at once.

I didn't even try to debug your regexes, because that script makes my eyes hurt, and I figured this was the more pressing issue. If it still doesn't work, then we can get into your specific regexes, but this will at least solve what I think your problem was actually about.

EDIT: It occurs to me this can be done even more programmatically. Somewhere in the same class declare a pattern 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;
    }
}

Then, use it thusly:

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

Now, no matter how many patterns you want to add, you only have to add them in one place. You can also easily control the order in which the patterns are checked by only moving one line in the code.

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
0

Try using

if(p.equals(p1)) 

instead of

if(p==p1)

Because you are trying to compare strings.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • I don't think this'll help. p is a `Pattern`, not a `String`. – FrankieTheKneeMan Mar 19 '13 at 09:22
  • Be it `Pattern` or `String`, it all comes down to the fact that these aren't primitives - therefore `equals()` should be used. Otherwise, you'd be comparing references rather than the objects. – Quetzalcoatl Mar 19 '13 at 11:06