Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:48:12+00:00 2026-05-16T22:48:12+00:00

I am using the following Regular Expresion to remove html tags from a string.

  • 0

I am using the following Regular Expresion to remove html tags from a string. It works except I leave the closing tag. If I attempt to remove: <a href="blah">blah</a> it leaves the <a/>.

I do not know Regular Expression syntax at all and fumbled through this. Can someone with RegEx knowledge please provide me with a pattern that will work.

Here is my code:

  string sPattern = @"<\/?!?(img|a)[^>]*>";
  Regex rgx = new Regex(sPattern);
  Match m = rgx.Match(sSummary);
  string sResult = "";
  if (m.Success)
   sResult = rgx.Replace(sSummary, "", 1);

I am looking to remove the first occurence of the <a> and <img> tags.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:48:12+00:00Added an answer on May 16, 2026 at 10:48 pm

    Using a regular expression to parse HTML is fraught with pitfalls. HTML is not a regular language and hence can’t be 100% correctly parsed with a regex. This is just one of many problems you will run into. The best approach is to use an HTML / XML parser to do this for you.

    Here is a link to a blog post I wrote awhile back which goes into more details about this problem.

    • http://blogs.msdn.com/b/jaredpar/archive/2008/10/15/regular-expression-limitations.aspx

    That being said, here’s a solution that should fix this particular problem. It in no way is a perfect solution though.

    var pattern = @"<(img|a)[^>]*>(?<content>[^<]*)<";
    var regex = new Regex(pattern);
    var m = regex.Match(sSummary);
    if ( m.Success ) { 
      sResult = m.Groups["content"].Value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove following tag from string. [caption id=attachment_9 align=alignleft width=137 caption=test][/caption]
I am trying to extract links from HTML. I am using the following regular
Possible Duplicate: Using C# regular expressions to remove HTML tags Regex Pattern in C#
I'm using the following regular expression to march hrefs on a web page. \/static\/workout\/[A-Z]{1,4}032812[A-Z]{1,5}-EQB.html
I am using the following regular expression in .htaccess file RewriteRule ^js_embed/([a-zA-Z0-9\-_]+)/[0-9]+ /users/$1/js_embed_$2.html [R=301,L]
I am using the following regular expression (from http://www.simple-talk.com/dotnet/asp.net/regular-expression-based-token-replacement-in-asp.net/ ) (?<functionName>[^\$]*?)\((?:(?<params>.**?)(?:,|(?=\))))*?) it works fine,
I am currently using the following regular expression: ^[a-zA-Z]{0,}(\\*?)?[a-zA-Z0-9]{0,} to check a string to
I need to match and remove all tags using a regular expression in Perl.
I am attempting to parse a string like the following using a .NET regular
I've been playing around with retrieving data from a string using regular expression, mostly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.