正则表达式使用索引

----------------------------------------------------------------------

VS中

例子1: Format 32位多字符集,变成 64位unicode的时候,cstring接受的需要是宽字节字符。需要在“”前加上宏_T()

  //结果应该是这样
CString mm1; mm1.Format(_T("%d"),1);
  //替换前捕获应该是这样 CString mm2; mm2.Format("%d",2);

查找内容:
Format("{.*}"

替换内容:
Format(_T("1")

例子2:

/** 根据数据类型和名称删除组件数据。*/
bool deleteComponentData(const std::string& dataType, const std::string& name);

替换成

//根据数据类型和名称删除组件数据。
bool deleteComponentData(const std::string& dataType, const std::string& name);

在ctrl + H中编写替换公式

/** {.*}*/
替换为

//1

vs查找替换正则表达式 参考文档:https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(VS.REGULAREXPRESSIONBUILDER)&rd=true

 ----------------------------------------------------------------------

c#中使用

 运用正则表达式检索对应网站的标签值。需要注意的是,提取按照对应分组名称提取需要先匹配,每一行中再按分组名称获取。

{
  "SrvAddr": "http://c.ishadow.host/",
  "RegexPattern": "<h4>.*?服务器地址:(?<SrvAddr>.*?)</h4>.*?<h4>端口:(?<SrvPort>.*?)</h4>.*?<h4>.*?密码:(?<SrvPass>.*?)</h4>.*?<h4>加密方式:(?<SrvMethod>.*?)</h4>",
},

 

protected override void Handle(Page page)
{
    try
    {
        RegexOptions options = RegexOptions.Singleline;
        MatchCollection regexMatches = Regex.Matches(page.Content, RegexPattern, options);
        Console.WriteLine(regexMatches.Count.ToString());


        List<SS_Server_Model> results = new List<SS_Server_Model>();
                
        foreach (Match match in regexMatches)
        {
            results.Add(new SS_Server_Model()
            {
                SrvAddr = match.Groups["SrvAddr"].Value,
                SrvPort = match.Groups["SrvPort"].Value,
                SrvPass = match.Groups["SrvPass"].Value,
                SrvMethod = match.Groups["SrvMethod"].Value,
                SrvRemark = match.Groups["SrvRemark"].Value,
            });
        }

        page.AddResultItem("results", results);


        //后续采集网页
        //page.AddTargetRequest(new Request("",null));
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
            
}

--------------------------------------------------------------------------------

RegexTest 正则表达式工具

正则中使用分组匹配数据 

30分钟正则 http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html

(?<name>.*)

在编程中需要拿到这组数据

MatchCollection regexMatches = Regex.Matches(page.Content, "(?<name>.*)");
List<SS_Server_Model> results = new List<SS_Server_Model>();
foreach (Match match in regexMatches)
{
    results.Add(new SS_Server_Model() {
        SrvName = match.Groups["name"].Value,
    });
}

在替换工具中使用这组符号

${name}

原文地址:https://www.cnblogs.com/Again/p/6387597.html