我想用模式匹配输入文本(来自用户)。
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");
}
请帮我写这个鳕鱼