提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
正则表达式中的反斜杠
import java.util.regex.Matcher; import java.util.regex.Pattern; //n o w j a v a . c o m 提供 public class Main { public static void main(String[] argv) { String rep = "a\\\\b"; String input = "> XYZ <=> ABC <"; Pattern pattern = Pattern.compile("ABC|XYZ"); Matcher matcher = pattern.matcher(input); System.out.println(matcher.replaceFirst(rep)); System.out.println(matcher.replaceAll(rep)); rep = "\\\\r\\\\n"; input = "line 1\nline 2\nline 3\n"; pattern = Pattern.compile("\\n"); matcher = pattern.matcher(input); System.out.println(""); System.out.println("Before:");