PatternSyntaxException:在java中使用String.ReplaceAll函数时?

时间:2014-10-10 06:44:58

标签: java regex string junit

当我尝试执行String.ReplaceAll时,我得到了一个patternyntax异常。

以下是我的字符串

Number of testcases to execute : 39?    Starting execution of test case : testCreateDAta?    The class that has completed its execution is : test.com.mySpace.service.ejb.session.MyTest?     Finished execution of test case : testCreateDAta?    Starting execution of test case : testUpdate?

我想做的事情:

junitReportString.replaceAll("?", "\n"); 

上面的代码提示我以下异常:

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0 

然后我再次尝试使用以下代码:

junitReportString.replaceAll("\\?", "\n"); 

上面的代码提取了我上面提到的相同字符串。

我的完整代码:

String junitReportString=new String();
myApp= new URL("http://localhost:port/testWeb/TestJunit.jsp");
URLConnection yc = myApp.openConnection();
yc.connect();
junitReportString=yc.getHeaderField("finalJUNITReport").toString();
System.out.println(junitReportString);
junitReportString.replaceAll("\\?", "\n");
System.out.println("Report Details     ==============>"+junitReportString);

我的代码出了什么问题。任何指导都非常赞赏。

1 个答案:

答案 0 :(得分:3)

#replaceAll不会更改实际的String,只会返回包含更改的新String

junitReportString=junitReportString.replaceAll("\\?", "\n"); 
//Now you have changed String