Solved

Remove parts of string using C# or Regular Expression

Posted on 2012-03-12
3
646 Views
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,

Rit
0
Comment
Question by:rito1
3 Comments
 
LVL 4

Accepted Solution

by:
petr_hlucin earned 500 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:rito1
Comment Utility
Perfect, thank you very much indeed.

Rit
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Try this:

using System.Text.RegularExpressions;

...

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

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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!
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now