[正则表达式]正则表达式无法匹配,why?

会飞的一棵树 2017-11-23 02:28:29
想用正则表达式去匹配一下内容:
<p id="lId1" class="lrcItem">
<p id="lId2" class="lrcItem">
<p id="lId3" class="lrcItem">
然后把其中的数字替换掉。
列如:String s = "<p id=\"lId1\" class=\"lrcItem\">";
于是写了正则表达式:
String regex="<p id=\"lId\\d+\" class=\"lrcItem\">";
String s = s.replaceAll(regex,a);
但是输出的s却没有任何内容被替换。
...全文
316 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hugechun 2017-12-26
  • 打赏
  • 举报
回复
String s = "<p id=\"lId1\" class=\"lrcItem\">"; s = s.replaceAll("\\d","a"); System.out.println(s);
xy825459556 2017-12-26
  • 打赏
  • 举报
回复
String s = "<p id=\"lId1\" class=\"lrcItem\">"; String regex="<p id=\"lId\\d+\" class=\"lrcItem\">"; s = s.replaceAll(regex,"a"); System.out.println(s);
「已注销」 2017-12-25
  • 打赏
  • 举报
回复
若鱼1919 2017-11-23
  • 打赏
  • 举报
回复

public static void main(String[] args) {
		String str = "<p id=\"lId1\" class=\"lrcItem\">\r\n" + 
				"<p id=\"lId2\" class=\"lrcItem\">\r\n" + 
				"<p id=\"lId3\" class=\"lrcItem\">";
		str = str.replaceAll("lId\\d", "lId");
		System.out.println(str);
	}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧