Remove parts of string using C# or Regular Expression

Posted on 2012-03-12
Last Modified: 2012-03-12
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,

Question by:rito1

Accepted Solution

petr_hlucin earned 500 total points
ID: 37710063

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, "");

Author Closing Comment

ID: 37710082
Perfect, thank you very much indeed.

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37710088
Try this:

using System.Text.RegularExpressions;


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

Open in new window


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now