2

I have a given string:

"Hi there <ss type="laugh">:)</ss>"

When I'm using

Regex.Replace(s, @"<(.|\n)*?>", string.Empty);

It returns me

"Hi there :)"

How can i modify the Expression to remove all "ss" tags and everything between them?

1
  • something like @"<ss(.|\n)*?ss>"? Commented Oct 23, 2015 at 22:50

1 Answer 1

1

The string contains a regular XML tag, thus, you can make use of XElement.

This code will just keep the text of the outer element (text outside tags):

var s = "Hi there <ss type=\"laugh\">:)</ss>";
var el = XElement.Parse(string.Format("<root1>{0}</root1>", s));
var result = string.Concat(el.Nodes().OfType<XText>().Select(t => t.Value)).Trim();

enter image description here

Just make sure to use System.Xml.Linq namespace.

If you have other XML tags and you just want to remove ss tags:

var s = "<b>Hi</b> there <ss type=\"laugh\">:)</ss>";
var el = XElement.Parse(string.Format("<root1>{0}</root1>", s));
el.Descendants("ss").Remove();
var reader = el.CreateReader();
reader.MoveToContent();
var result2 = reader.ReadInnerXml().Trim();

Result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.