正则表达式 - [正则][获取<a>标签前后所有的正文内容][java]
高洛峰
高洛峰 2017-04-18 09:15:14
0
1
596

内容例子:
哥伦比亚著名作家加西亚·马尔克斯的长篇小说《百年孤独》发表

内容组成:由n组 AAAABBBBBBCCCCC形式的内容组成的字符串
例如:

AAAA<a>BBBBBB</a>CCCCC
AAAA<a>BBBBBB</a>CCCCCAAAA<a>BBBBBB</a>CCCCC
AAAA<a>BBBBBB</a>CCCCCAAAA<a>BBBBBB</a>CCCCCAAAA<a>BBBBBB</a>CCCCC

最终取得的内容:
AAAABBBBBCCCCC
AAAAAAABBBBBBBCCCCCCAAAABBBBBCCCC

这样子;

这个正则在java里怎么写? 多谢;

最好一个正则不分组直接获取最终内容.

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
迷茫

How is the text content determined? At present, it seems that all you need to do is delete the <a></a> tag. This is not difficult, right?


[Supplement]If it’s just text + a tag, just use as the basic regular expression. </?a>But I am not very familiar with Java. The code generated online is as follows. It seems that it only matches but does not replace. I hope you can change it. . .

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
    
    public static void main(String args[]) {
        String str = "";
        String pattern = "\<\/?a\>";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template