为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 188 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信