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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

738 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