Java 替换所有方法抛出模式SyntaxException
请看以下代码:Java 替换所有方法抛出模式SyntaxException,java,regex,string,replaceall,patternsyntaxexception,Java,Regex,String,Replaceall,Patternsyntaxexception,请看以下代码: String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWon
String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWong(T1281954U005) and Status is INCMP on 10-JUN-11 11.28.45.764386 PM -05:00";
//comment = comment.replaceAll("\\)", "\\\\)");
//comment = comment.replaceAll("\\(", "\\\\(");
if(comment == null || comment.length() < 100)
{
System.out.println();
}
String[] strArray = comment.split(" ");
for (int i = 0; i < strArray.length; i++)
{
if(strArray[i].length() > 100)
{
int iter = strArray[i].length() / 100 ;
int count = 100 ;
int initCount = 0 ;
String strReplace = null;
for(int j =0 ; j< iter ; j++)
{
strReplace = strArray[i].substring(initCount ,count);
String strToReplace = strReplace + "\n" ;
comment = comment.replaceAll(strReplace,strToReplace);
//comment = comment.replaceAll("\\)", "\\\\)");
//comment = comment.replaceAll("\\(", "\\\\(");
//comment = comment.replaceAll("\\\\", "");
System.out.println(comment);
System.out.println(comment.contains("\n"));
initCount = count; //+1 ;
count = count +100 ;
}
}
}
}
<代码>字符串评论=“1”FRCK在香港2)本地收费将在香港PAY和3港元)预订:virginiawong@fahkco.com.hk4)FCR和文档:emilywu@fahkco.com.hkTel:00852-23021977传真:00852-2730217交易865320提交给Irginiawong(T1281954U005),状态为2011年6月10日11.28.45.764386下午-05:00的INCMP”;
//comment=comment.replaceAll(“\\”,“\\\)”;
//comment=comment.replaceAll(“\\(”,“\\\(”);
if(comment==null | | comment.length()<100)
{
System.out.println();
}
String[]strArray=comment.split(“”);
对于(int i=0;i当我运行时,会出现以下异常:
索引4附近的线程“main”java.util.regex.PatternSyntaxException:Unmatched closing')中出现异常
港币(0.3元)订票:virginiawong@fahkco.com.hk4)FCR和文档:emilywu@fahkco.com.hkTel:00852-
23021977传真:00852-2 根据我的理解,我必须逃离偏执狂“(”,“)”, 我尝试这样做(查看代码中的注释部分)没有任何异常,但我附加到字符串的换行符似乎没有出现。使用正则表达式作为第一个参数,而像
)
这样的字符在解释为正则表达式时具有特殊意义
试试看。(它仍然替换给定子字符串的所有匹配项。)对第一个参数使用正则表达式,并且像)
这样的字符在解释为正则表达式时具有特殊意义
试试看。(它仍然替换给定子字符串的所有匹配项。)没有正确回答您的问题。也许您可以将其表示为一个单元测试,以显示您期望的结果。@Pramod Kumar代码的目的是它将拆分一个以空格作为分隔符的字符串,如果任何字符串拆分数组超过100个字符,我将插入一个换行符(\n)并用原始字符串替换此新字符串。我的意思是,当您删除上述代码中的注释时,我不会得到异常,但会得到换行符(\n)当我最后打印字符串时,没有出现“am追加”。没有正确回答您的问题。也许您可以将其表示为一个单元测试,以显示您期望的结果。@Pramod Kumar代码的目的是它将拆分一个以空格作为分隔符的字符串,如果任何字符串拆分数组超过100个字符,我将插入一个换行符(\n)并将此新字符串替换为原始字符串。我的意思是,当您删除上述代码中的注释时,我不会得到异常,但当我最终打印字符串时,不会出现正在追加的换行符(\n)。