什么是这个ironpython代码的C#版本

Bay*_*len 1 c# regex ironpython

我需要在C#中编写这个IronPython代码(我找不到类似的C#库来匹配IronPython的re模块):

for v in variables:
  replace = re.compile(v, re.IGNORECASE)...
  re.sub(v, str(self.SQLVariables[v.upper().replace("&","")]),script_content)...
Run Code Online (Sandbox Code Playgroud)

换句话说,C#与以下表达式等效:

  • re.compile(...)...
  • 应用re.sub(...)...

Dav*_*nan 7

您的问题归结为,如何在C#中使用正则表达式?

答案就是Regex上课.要执行替换,您需要Regex.Replace().无需显式编译正则表达式,因为这是在创建Regex实例时完成的.


MSDN中的以下示例说明了如何使用该类:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is   text with   far  too   much   " + 
                     "whitespace.";
      string pattern = "\\s+";
      string replacement = " ";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);                             
   }
}
// The example displays the following output:
//       Original String: This is   text with   far  too   much   whitespace.
//       Replacement String: This is text with far too much whitespace.
Run Code Online (Sandbox Code Playgroud)