请帮我!我正在使用正则表达式用 Java 编写程序。我有很多句子结构。
给定句子 - “为 Ahmed Mohamed 医生写‘学习 java’ 的书”或“最好的书名:为 Ahmed Mohamed 学习 java”,等等。
这意味着:
(书)可以是[书或文本:或(文本)]。
(对于医生)可以是[为作者或为或由或为医生]。
正则表达式是:
"(?<=(book| the book| \\ (\\)|\\:)) .*? (?=(for doctor| for| for author))"
输出:
我想在(书)和(医生)之前提取任何单词并将其命名为标题。并在(对于医生)之后提取任何单词并将其命名为作者。
String inputtext = "book 'learning java' for doctor ahmed mohamed";
Pattern p = Pattern.compile("(?<=(book| the book| \\ (\\)|\\:)) .*? (?=(for doctor| for| for author))");
Matcher m = p.matcher(inputtext);
if (m.matches()) {
String author = m.group(1).trim();
String bookTitle = m.group(2).trim();
System.out.println("Title is : " + author);
System.out.println("Author is : " + bookTitle);
这个程序在运行时给了我错误,我会支持正则表达式。