请教正则表达式字符串分割

yuepengfei 2018-03-09 03:22:19
有如下字符串:
<a>1</a><b>2</b><c>3</c><a>11</a><b>22</b><c>33</c><a>111</a><b>222</b><c>333</c>

如何利用正则表达式得到

[0]=<a>1</a><b>2</b><c>3</c>
[1]=<a>11</a><b>22</b><c>33</c>
[2]=<a>111</a><b>222</b><c>333</c>


思路感觉应该是通过<a></c>进行处理,不会写正则,还请大家帮忙,谢谢
...全文
346 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2018-03-09
  • 打赏
  • 举报
回复
<a>.*</c>
oyljerry 2018-03-09
  • 打赏
  • 举报
回复

String content = "<a>1</a><b>2</b><c>3</c><a>11</a><b>22</b><c>33</c><a>111</a><b>222</b><c>333</c>";
        Pattern test_ptn = Pattern.compile("<a>.*?</c>");
        Matcher m1 = test_ptn.matcher(content);
        while (m1.find()) {
            System.out.println("text=" + m1.group(0));
        }
  • 打赏
  • 举报
回复
ArrayList<String> list = new ArrayList<String>(); Matcher m =Pattern.compile("<a>\\d+</a><b>\\d+</b><c>\\d+</c>").matcher("<a>1</a><b>2</b><c>3</c><a>11</a><b>22</b><c>33</c><a>111</a><b>222</b><c>333</c>"); while(m.find()) { for(int j = 0; j <= m.groupCount(); j++){ list.add(m.group(j)); System.out.print("[" + m.group(j) + "]"); System.out.println(); } } Object [] strs = list.toArray();

62,614

社区成员

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

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