正則表達式使用索引


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

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}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM