正则表达式替换文本

时间:2010-03-11 16:28:12

标签: c# regex

我需要一个正则表达式来替换字符串。

<span class=\"Translation\" lang=\"ThisLanguage\">

这一个:

<span class=\"Translation\" lang=\"ThisLanguage\" onDblClick=\"window.external.MyFunction(ThisLanguage)\">

此字符串中有多种语言,每种语言都包含不同的“ThisLanguage”

谁知道怎么做?

我正在使用C#.Net

谢谢!

4 个答案:

答案 0 :(得分:1)

通常不建议使用regexp解析HTML,因为HTML 不是常规的并且有足够的边缘情况可以使除了最简单的场景之外的所有场景都绊倒。对于除了最简单的例子之外的所有例子,我宁愿通过HTML解析器解析HTML并通过合适的API(例如DOM)对其进行操作

答案 1 :(得分:1)

有点冗长但Expresso节省了大量时间!

//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Thu, Mar 11, 2010, 04:37:21 PM
///  Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  <span.*?class="
///      <span
///      Any character, any number of repetitions, as few as possible
///      class="
///  [1]: A numbered capture group. [.*?]
///      Any character, any number of repetitions, as few as possible
///  ".*?lang="
///      "
///      Any character, any number of repetitions, as few as possible
///      lang="
///  [2]: A numbered capture group. [.*?]
///      Any character, any number of repetitions, as few as possible
///  ">
///      ">
///  
///
/// </summary>
public static Regex regex = new Regex(
      "<span.*?class=\"(.*?)\".*?lang=\"(.*?)\">",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


// This is the replacement string
public static string regexReplace = 
      "<span class=\"$1\" lang=\"$2\" onDblClick=\"window.external."+
      "MyFunction(ThisLanguage)\">\r\n";


//// 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();

答案 2 :(得分:0)

使用正则表达式解析HTML就像是第10个地狱圈。我没骗你。你最好tidy(不确定.NET是否整洁),然后通过XML解析器运行它。这样,您就可以提取classlang等特定属性,然后向onDblClick节点添加名为span的新属性。

否则,一种天真的方法(不确定.NET中的语法是什么,但这是在Perl中):

$str =~ s/<span\(.*?\)lang=\\"\(.*?\)\\">/<span$1lang=\\"$2\\" onDblClick=\\"window.external.MyFunction($2)\\">/

这里重要的是要匹配的模式(包括捕获):

<span\(.*\)lang=\\"\(.*?\)\\">

这匹配<span后跟任何内容,后跟lang=\"匹配\"之间的任何内容,然后是\">

替换模式是:

<span$1lang=\\"$2\\" onDblClick=\\"window.external.MyFunction($2)\\">

这会创建<span,后跟与lang$1)匹配的所有内容,然后是lang=\",后跟其捕获的语言名称($2 ),然后是onDblClick的东西。

我不熟悉.NET,所以你必须转换它。但它不应该太不同。您可能需要将\(更改为((具体取决于语法)。此外,我不确定.NET如何处理反向引用,但它应该是$1$2(就像在Java中一样)。

注意:我没有测试过这个!

答案 3 :(得分:0)

我不会使用正则表达式。我会用jQuery。

// set the lang value to ThisLanguage
$('span.Translation').attr('lang', 'ThisLanguage'); 
// add the onDblClick event with the value
$('span.Translation').attr('onDblClick', 'window.external.MyFunction(ThisLanguage)'); 

或者,如果您只是生成字符串(您可能不是,但 IF ,您可以这样做)并推出它们为什么不这样做?

string spanTag = String.Format("<span class=\"Translation\" lang=\"{0}\" onDblClick=\"window.external.MyFunction({0})\">", "ThisLanguage");
相关问题