我想在 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
}
}
- 2 回答
- 0 关注
- 188 浏览
添加回答
举报
0/150
提交
取消