我用String的replaceAll方法的时候,发觉只要被替换的字符串里面有关键字例如括号()的时候就不能正常处理,怎么办 这个问题在正则表达式里面也有出现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 replaceAll的第一个参数是个规则表达式,不是个一般的字符串,比如:s.replaceAll(".", 'a');就是把任何字符都换成'a'。 只要加个反斜杠就能转义:比如:s.replace("\\.", "a");之所以需要两个反斜杠,是因为首先编译器在编译时需要对字符串转义,然后才是运行时规则表达式的转义。 按此方式使用: public static void main(String[] args) { String str = "20050936080(6200)50906"; System.out.println(str.replaceAll("\\(6200\\)", "aaa")); } 现在的问题是我要替换的部分并没有括号,括号是在被替换以外的部分,如下我的代码: String regexp = "<textarea wrap=virtual id='txtContent' name='txtContent'"; String originalStr = "<tr style='height:30px;border-bottom:none;width:100%'>"; originalStr = originalStr + "<td class='personHeadTitle_TD' style='color:black;font-size:9pt;' colspan='1' rowspan='1' align='center' valign='middle' width='15%' height='30' nowrap> <b><font color=red size=3 face=宋体>标题</font></b></td>"; originalStr = originalStr + "<td class='textareaContent_frstLine' style='color:black;font-size:9pt;' colspan='5' rowspan='1' align='left' valign='middle' width='85%' height='30' nowrap> kmonkey4</td>"; originalStr = originalStr + "</tr>"; originalStr = originalStr + "<tr style='height:120px;border-bottom:none;width:100%'>"; originalStr = originalStr + "<td class='personHeadTitle_TD' style='color:black;font-size:9pt;' colspan='1' rowspan='1' align='center' valign='middle' width='15%' height='120' nowrap> <b><font color=red size=3 face=宋体>申请内容</font></b></td>"; originalStr = originalStr + "<td class='textareaContent_frstLine' style='color:black;font-size:9pt;' colspan='5' rowspan='1' align='left' valign='top' width='84%' height='120' nowrap>"; originalStr = originalStr + " <textarea wrap=virtual id='txtContent' name='txtContent' type='textarea' style='border:none;width:99%;height:120px;overflow:hidden' readOnly >kmonkey4"; originalStr = originalStr + "asdlfjs(a dj fljdsa)fa</textarea>"; originalStr = originalStr + "</td>"; originalStr = originalStr + "</tr>"; originalStr = originalStr + "<!--ReplacedByContent-->"; originalStr = originalStr + "<tr style='height:180px;border-bottom:none;width:100%'><td>asdfsdfsaf</td></tr>"; try { Pattern p = Pattern.compile(regexp); //前面的字符串 String preStr = p.split(originalStr)[0]; //后面的字符串 String lastStr = p.split(originalStr)[1]; //前面最接近<tr的位置,即tr开始的地方 int beginTrPosition = preStr.lastIndexOf("<tr"); System.out.println("tr开始的地方"+beginTrPosition); //后面最接近</tr>的位置,即tr相对结束的地方 int endTrPosition = lastStr.indexOf("</tr>"); System.out.println("tr相对结束的地方"+endTrPosition); //求tr真正结束的地方,要加上preStr和regexp的长度再加</tr>的五个字符长度 int endTrPosition2 = preStr.length()+regexp.length()+ endTrPosition+5; String wantedStr = originalStr.substring(beginTrPosition,endTrPosition2); p = p.compile(wantedStr); Matcher m = p.matcher(originalStr); String s = m.replaceAll(""); System.out.println("the s is:===="+s); p = p.compile("<!--ReplacedByContent-->"); m = p.matcher(s); System.out.println(m.replaceAll(wantedStr)); } catch (Exception ex) { System.out.println(ex.getMessage()); } java学习ingSystem.out.println(ex.getMessage()); 变量wantedStr 中包含了括号(),该怎么处理 就是说,在Pattern预编译compile的时候,有没有什么选项可以忽略那些保留关键字 java中如何编程提取摄像头中的图像啊? 大家帮我做套题目吧 急用啊!!!!!!!!!! 真是不明白是什么原因??? 内部类怎样调用外部类的方法呢? 请各位指证错误,同时阐述道理.谢谢!! 异常 谁有这样的例子,我给500分 jb7在设置server config时,没有ok按钮,只有reset to default按钮 哪位仁兄研究过 jndi_turorial 进来帮帮忙好吗? 请问哪里有JAVA的新应用的资料。多谢! 如何将两个ArrayList连接起来成为一个ArrayList 小弟初学jsp跟sql2000的连接,出现连接问题不会解决,请帮忙。
s.replaceAll(".", 'a');就是把任何字符都换成'a'。
s.replace("\\.", "a");
之所以需要两个反斜杠,是因为首先编译器在编译时需要对字符串转义,然后才是运行时规则表达式的转义。
public static void main(String[] args) {
String str = "20050936080(6200)50906";
System.out.println(str.replaceAll("\\(6200\\)", "aaa"));
}
如下我的代码: String regexp = "<textarea wrap=virtual id='txtContent' name='txtContent'"; String originalStr = "<tr style='height:30px;border-bottom:none;width:100%'>";
originalStr = originalStr + "<td class='personHeadTitle_TD' style='color:black;font-size:9pt;' colspan='1' rowspan='1' align='center' valign='middle' width='15%' height='30' nowrap> <b><font color=red size=3 face=宋体>标题</font></b></td>";
originalStr = originalStr + "<td class='textareaContent_frstLine' style='color:black;font-size:9pt;' colspan='5' rowspan='1' align='left' valign='middle' width='85%' height='30' nowrap> kmonkey4</td>";
originalStr = originalStr + "</tr>";
originalStr = originalStr +
"<tr style='height:120px;border-bottom:none;width:100%'>";
originalStr = originalStr + "<td class='personHeadTitle_TD' style='color:black;font-size:9pt;' colspan='1' rowspan='1' align='center' valign='middle' width='15%' height='120' nowrap> <b><font color=red size=3 face=宋体>申请内容</font></b></td>";
originalStr = originalStr + "<td class='textareaContent_frstLine' style='color:black;font-size:9pt;' colspan='5' rowspan='1' align='left' valign='top' width='84%' height='120' nowrap>";
originalStr = originalStr + " <textarea wrap=virtual id='txtContent' name='txtContent' type='textarea' style='border:none;width:99%;height:120px;overflow:hidden' readOnly >kmonkey4";
originalStr = originalStr + "asdlfjs(a dj fljdsa)fa</textarea>";
originalStr = originalStr + "</td>";
originalStr = originalStr + "</tr>";
originalStr = originalStr + "<!--ReplacedByContent-->";
originalStr = originalStr + "<tr style='height:180px;border-bottom:none;width:100%'><td>asdfsdfsaf</td></tr>";
try {
Pattern p = Pattern.compile(regexp);
//前面的字符串
String preStr = p.split(originalStr)[0];
//后面的字符串
String lastStr = p.split(originalStr)[1];
//前面最接近<tr的位置,即tr开始的地方
int beginTrPosition = preStr.lastIndexOf("<tr");
System.out.println("tr开始的地方"+beginTrPosition);
//后面最接近</tr>的位置,即tr相对结束的地方
int endTrPosition = lastStr.indexOf("</tr>");
System.out.println("tr相对结束的地方"+endTrPosition);
//求tr真正结束的地方,要加上preStr和regexp的长度再加</tr>的五个字符长度
int endTrPosition2 = preStr.length()+regexp.length()+ endTrPosition+5; String wantedStr = originalStr.substring(beginTrPosition,endTrPosition2); p = p.compile(wantedStr);
Matcher m = p.matcher(originalStr); String s = m.replaceAll("");
System.out.println("the s is:===="+s);
p = p.compile("<!--ReplacedByContent-->");
m = p.matcher(s); System.out.println(m.replaceAll(wantedStr)); }
catch (Exception ex) {
System.out.println(ex.getMessage());
}