亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

html文件中的c#正則表達式匹配部分

html文件中的c#正則表達式匹配部分

C#
catspeake 2021-08-22 14:56:27
我想在 c# 中使用正則表達式來匹配某些內容。例如,輸入字符串如下:7687687toyi7fy<body style="box-sizing: border-box; height: inherit; width: inherit; margin: 0px; overflow: hidden">lkjlknkjyyugtfiytfif</body></html>現在我想匹配<body ... hidden>和</body> 所以對于上面的例子,我想匹配“lkjlknkjyyugtfiytfif”我嘗試使用該模式,<body(.+?)>(.+?)</body>但不知何故它不匹配任何內容。對于調試,我也嘗試使用<body(.+?)>,它匹配<body ... hidden>成功,但是無論我在 之后添加什么<body(.+?)>,我都無法得到我想要的。任何建議將不勝感激。
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

由于我的評論有幫助,我也會在這里發布,供其他人查看:


你可以在這里看到這個


為了方便閱讀:


using System;

using System.Text.RegularExpressions;

class Program

{

    static void Main(string[] args)

    {


         string r = 

            @"<(?'tag'\w+?).*>"      +   // match first tag, and name it 'tag' 

            @"(?'text'.*?)"          +   // match text content, name it 'textd' 

            @"</\k'tag'>";               // match last tag, denoted by 'tag' 


         string text = "<h1>hello</h1>"; 


         Match m = Regex.Match (text, r); 

         Console.WriteLine (m.Groups ["tag"]);                // h1 

         Console.WriteLine (m.Groups ["text"]);               // hello 

    }

}


查看完整回答
反對 回復 2021-08-22
  • 2 回答
  • 0 關注
  • 222 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號