在我的 Java 程序中,我试图替换在字符串 ( paloalto\to sanjose\)中包含反斜杠的子字符串。但是,即使我使用双反斜杠来减轻反斜杠作为转义字符的影响,我仍然收到以下错误:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 9
paloalto\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at Main.main(Main.java:8)
下面是我的代码:
String original = "paloalto\\adam paloalto\\jack paloalto\\justin";
System.out.println("original = " + original);
String sanjose = original.replaceAll("paloalto\\", "sanjose\\");
System.out.println("backgammon = " + sanjose);
跃然一笑
弑天下
jeck猫
相关分类