如何在C#中使用Regexp替换<;img>;带有它的HTML标记';指定字符串中的s ALT参数
我有一个字符串,其中包含许多与此结构完全相同的HTML标记:如何在C#中使用Regexp替换<;img>;带有它的HTML标记';指定字符串中的s ALT参数,c#,.net,html,regex,C#,.net,Html,Regex,我有一个字符串,其中包含许多与此结构完全相同的HTML标记: Hello <img src="./images/foo.gif" alt="bar"> World! <img src="./images/foo2.gif" alt="bar2"> 我使用C#和.NET Framework使用,我设计了以下表达式: <img src="[^"]*" alt="([^"]*)"> 这显然需要生成的标记与您的示例非常精确地匹配,因此生成标记的内容中的任何更改
Hello <img src="./images/foo.gif" alt="bar"> World!
<img src="./images/foo2.gif" alt="bar2">
我使用C#和.NET Framework使用,我设计了以下表达式:
<img src="[^"]*" alt="([^"]*)">
这显然需要生成的标记与您的示例非常精确地匹配,因此生成标记的内容中的任何更改都会导致此中断。出于这个原因,我建议你考虑使用一些代替ReGEX的方法来解决这个问题。
下面是Expresso通过大量使用示例生成的代码
// 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();
//使用System.Text.RegularExpressions;
///
///为C#on构建的正则表达式:2010年11月22日星期一下午3:51:18
///使用Expresso版本:3.0.2766,http://www.ultrapico.com
///
///正则表达式的说明:
///
///
/// ">
///
///
///
公共静态正则表达式Regex=新正则表达式(
"",
RegexOptions.Compiled
);
//这是替换字符串
公共静态字符串regexReplace=“$1”;
////使用替换模式替换InputText中匹配的文本
//字符串结果=regex.Replace(InputText,regexReplace);
////在正则表达式匹配的位置拆分InputText
//string[]results=regex.Split(InputText);
////捕获InputText中的第一个匹配项(如果有)
//匹配m=正则表达式匹配(InputText);
////捕获InputText中的所有匹配项
//MatchCollection ms=regex.Matches(InputText);
////测试以查看InputText中是否存在匹配项
//bool IsMatch=regex.IsMatch(InputText);
////获取所有命名和编号的捕获组的名称
//字符串[]GroupNames=regex.GetGroupNames();
////获取所有命名和编号的捕获组的编号
//int[]GroupNumbers=regex.GetGroupNumbers();
不仅感谢您提供的解决方案,还感谢您提供了一个可以在将来省去麻烦的好工具
// 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();