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.