• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

Remove parts of string using C# or Regular Expression

Hi All,

I have a string variable like so:

string strDesc = "<span title=More of something here>Something here</span>"

This variables content can differ but the structure stays the same i.e.
"<span title=*>*</span>"

Could anyone suggest how I could strip out the following elements from the string just leaving the contents of the span's title attribute..

remove - "<span title=" and ">*</span>"

Many thanks,

Rit
0
rito1
Asked:
rito1
1 Solution
 
petr_hlucinCommented:
Hello,

try the following code:

string strDesc = "<span title=More of something here>Something here</span>";
Regex regex = new Regex("<span title=");
strDesc = regex.Replace(strDesc, "");
regex = new Regex(">[^<]*</span>");
strDesc = regex.replace(strDesc, "");
0
 
rito1Author Commented:
Perfect, thank you very much indeed.

Rit
0
 
käµfm³d 👽Commented:
Try this:

using System.Text.RegularExpressions;

...

string result = Regex.Replace(input, "<span [^>]*?title=\"([^\"]+)[^>]*>(?:[^<]|<(?!/span>))*</span>", "$1");

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now