我正在尝试使用正则表达式来解析与此类似的字符串:
<tag>Text that is written here. This could happen here: <Inline::~Inline> </tag>
如何使用正则表达式替换内联中的“<”和“>”字符。问题是 Inline 可能有不同的名称,并且在主标记内可以看到诸如“< 20”之类的比较。
谢谢。
最佳答案
假设内联标签:
- 两次出现相同的字符串,其中一次前面有波浪号
- 始终有两个冒号
那么这应该可以工作:
String s= "<tag>some text here > 20 <test::~test> and then </tag>";
s = s.replaceAll("<((\\w+)::~\\2)>","$1");
System.out.println(s);
无论内联/测试被称为什么。
已编辑 - 忘记放入两个冒号:-)
关于Java正则表达式匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9294230/