我试图用正则表达式解析字符串像类似于这样:的Java REG EXP匹配模式
<tag>Text that is written here. This could happen here: <Inline::~Inline> </tag>
如何使用正则表达式从内联替换“<”和“>”字符。问题在于Inline可能有不同的名称,在主标签内可以看到比较结果,如'< 20'。
谢谢。
我试图用正则表达式解析字符串像类似于这样:的Java REG EXP匹配模式
<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);
无论调用什么Inline/Test。
EDITED - 忘了把两个冒号放在:-)
这种模式应该匹配任何标签:
<(.|\n)*?>
所以对于内嵌匹配:因为我觉得这是一个答案值得
<Inline(.|\n)*?>
,我写它作为一个答案,而不是评论:
RegEx match open tags except XHTML self-contained tags当然第一个answer的问题。 所以,要说清楚和简短:甚至不要尝试使用正则表达式。
+1,但我假设这不是XHTML – 2012-02-15 14:11:35
你能更具体地关于不同的Inline名称吗?它是否接近名单?它是否接近格式,例如'<东西::〜事>'? – AlexR 2012-02-15 13:28:16
我不确定可能的值,但格式相同 – Radu 2012-02-15 13:29:42
在这种情况下,请将user1168884的答案标记为正确,因为它*为*。 – AlexR 2012-02-15 14:00:58