【问题标题】:how to define regular expression in c#如何在c#中定义正则表达式
【发布时间】:2012-05-30 19:55:52
【问题描述】:

我需要使用正则表达式替换源代码文档中的所有字符串,如下所示:
strcpy(x,"string is string") 变为 _tcscpy(x,_T("string is string"));
实际上,我需要在函数中用 T_marco 包围所有字符串
如何在 c# 中定义正则表达式模式来做到这一点?
谢谢

【问题讨论】:

    标签: c# regex migration migrate


    【解决方案1】:

    如果您需要这样做,您可以简单地从编辑菜单运行替换,使用要替换的strcpy(x,"string is string")_tcscpy(x,_T("string is string")); 作为替换,设置“整个项目”。

    【讨论】:

    • 实际上,我需要用 T_ marco 包围我的字符串“字符串是字符串”。
    【解决方案2】:

    首先,在你的类中导入正则表达式的 API 以供使用:

    using System.Text.RegularExpressions;
    

    在你的方法中,实例化新的正则表达式:

    Regex regexName = new Regex(@"string of regexExpression", RegexOptions.IgnoreCase);
    

    第三,你分析你的字符串或提取你喜欢的字符串部分:

    MatchCollection nameOfResult = regexName.Matches(this.yourString);
    foreach (Match result in nomeOfResult)
            {
                System.out.println(result.ToString());
            }
    

    第三个,如果你替换部分字符串对应你的正则表达式:

    Regex.Replace(yourString, regexName);
    

    试试这个模式:strcpy.*x,.*string.*is.string. 对于模式或测试模式,请使用此:http://rubular.com/ 或此:http://myregexp.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多