import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\n(?=[^\"]+?\"\\s*$)";
final String string = "ability,n. 能力,能耐;才能\n"
+ "able,adj. 能; 有能力的;能干的\n"
+ "about,\"prep. 关于;大约\n"
+ "n. 大致;粗枝大叶;不拘小节的人\n"
+ "adj. 在附近的;四处走动的;在起作用的\n"
+ "adv. 大约;周围;到处\"\n"
+ "above,\"prep. 超过;在……上面;在……之上\n"
+ "n. 上文\n"
+ "adj. 上文的\n"
+ "adv. 在上面;在上文\"\n"
+ "accident,n. 事故;意外; 意外事件;机遇\n"
+ "accurate,adj. 精确的\n"
+ "ache,\"n. 疼痛\n"
+ "vi. 疼痛;渴望\"\n"
+ "activity,n. 活动;行动;活跃\n"
+ "actor,n. 男演员;行动者;作用物\n"
+ "actress,n. 女演员\n"
+ "actually,adv. 实际上;事实上";
final String subst = " ";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html