猿问

html文件中的c#正则表达式匹配部分

我想在 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(.+?)>,我都无法得到我想要的。


任何建议将不胜感激。


catspeake
浏览 189回答 2
2回答

江户川乱折腾

由于我的评论有帮助,我也会在这里发布,供其他人查看:你可以在这里看到这个为了方便阅读:using System;using System.Text.RegularExpressions;class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string r =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"<(?'tag'\w+?).*>"&nbsp; &nbsp; &nbsp; +&nbsp; &nbsp;// match first tag, and name it 'tag'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"(?'text'.*?)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +&nbsp; &nbsp;// match text content, name it 'textd'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"</\k'tag'>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// match last tag, denoted by 'tag'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string text = "<h1>hello</h1>";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Match m = Regex.Match (text, r);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine (m.Groups ["tag"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // h1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine (m.Groups ["text"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// hello&nbsp;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答