0

I'm trying to use regexp to parse a string like similar to this:

<tag>Text that is written here. This could happen here: <Inline::~Inline> </tag>

How can I use regexp to replace the "<" and ">" characters from Inline. Problem is that Inline might have a different name and inside the main tag one can see comparisons such as '< 20'.

Thanks.

Radu
  • 1,044
  • 3
  • 12
  • 35

3 Answers3

3

Assuming that the inline tag:

  • has the same string in it twice, once preceded by a tilde
  • always has the two colons

then this should work:

    String s= "<tag>some text here > 20 <test::~test> and then </tag>";
    s = s.replaceAll("<((\\w+)::~\\2)>","$1");
    System.out.println(s);

regardless what Inline/Test is called.

EDITED - forgot to put TWO colons in :-)

matt freake
  • 4,877
  • 4
  • 27
  • 56
  • Regex grouping (i.e., the \\2 syntax) is pretty much the best thing since sliced bread! ;) – Andre Feb 15 '12 at 14:33
1

Since I think this is an answer worth, I write it as an answer instead of a comment:

RegEx match open tags except XHTML self-contained tags and of course the first answer to the question. So, to make it clear and short: Don't even try to use regular expressions.

Community
  • 1
  • 1
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

This pattern should match any tag:

<(.|\n)*?>

So for the inline matching:

<Inline(.|\n)*?>
James
  • 3,765
  • 4
  • 48
  • 79