user3458716
user3458716

Reputation:

Regex tags eliminate

I am trying to write a regex which will eliminate a particular tag but keep its value.

String s = "I am trying to eliminate tag link <link class < =abc>okay</link>"

or:

String s = "I am trying to eliminate tag link <link>okay</link>"

It is straightforward to replace the closing tag with regex = "</link>", but the start tag can contain any number of attributes.

The required output should be "I am trying to eliminate tag link okay"

I tried s = s.replaceAll("<link.*>$",""), but it replaces all the text after <link

Upvotes: 0

Views: 45

Answers (2)

Ibrahim
Ibrahim

Reputation: 6088

Try this code:

<\/?link.*?>

See the example here: https://regex101.com/r/yUVFpR/1

For the replaceAll function, try this:

s = s.replaceAll("<link>([^<]*)</link>","$1");

Upvotes: 0

user94559
user94559

Reputation: 60143

As a first approximation, <link[^>]*> should work. (It means to match <link followed by any number of characters that aren't >, followed by >.) But this approach will fail when a right angle bracket appears in an attribute value.

A much better approach is to not use regular expressions at all, but instead use an XML parser.

Upvotes: 1

Related Questions