Remove parts of string using C# or Regular Expression

Posted on 2012-03-12
Medium Priority
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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

762 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