开发者

How to use Regexp in C# to replace any occurence of <img> HTML tag with it's ALT parameter within the given string

开发者 https://www.devze.com 2023-01-26 15:52 出处:网络
I have string containing many of HTML tag of exactly that construction: Hello <img src=\"./images/foo.gif\" alt=\"bar\"> World!

I have string containing many of HTML tag of exactly that construction:

Hello <img src="./images/foo.gif" alt="bar"> World!
<img src="./images/foo2.gif" alt="bar2">

The string containing that tags is autogenerated from external tool and it is guaranteed to have exactly that declaration of tag

Now I wish to replace every occurence of such tag within given string with its "alt" parameter, so for the above sample results should be:

Hel开发者_Python百科lo bar World!
bar2

I am using C# and .NET Framework


Using Expresso, I've devised the following expression:

<img src="[^"]*" alt="([^"]*)">

This obviously requires the generated tag to match pretty exactly to your example, so any changes in what's generating the tags will cause this to break. For that reason, I'd advise you to consider using something like the HtmlAgilityPack instead of regex to solve this problem.

Here's the code that Expresso generated with a number of usage examples.

//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Mon, Nov 22, 2010, 03:51:18 PM
///  Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  <img src="
///      <img
///      Space
///      src="
///  Any character that is NOT in this class: ["], any number of repetitions
///  " alt="
///      "
///      Space
///      alt="
///  [1]: A numbered capture group. [[^"]*]
///      Any character that is NOT in this class: ["], any number of repetitions
///  ">
///      ">
///  
///
/// </summary>
public static Regex regex = new Regex(
      "<img src=\"[^\"]*\" alt=\"([^\"]*)\">",
      RegexOptions.Compiled
    );


// This is the replacement string
public static string regexReplace = "$1";


//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();
0

精彩评论

暂无评论...
验证码 换一张
取 消