正则表达式提取属性值

问题描述 投票:0回答:3
c# html regex
3个回答
40
投票

这个 C# 正则表达式将找到所有标题值:

(?<=\btitle=")[^"]*

C#代码是这样的:

Regex regex = new Regex(@"(?<=\btitle="")[^""]*");
Match match = regex.Match(input);
string title = match.Value;

正则表达式使用 Positive Lookbehind 来查找

title
值开始的位置。然后它会匹配所有内容直到结束双引号。


12
投票

使用下面的正则表达式

title="([^"]+)"

然后使用Groups浏览匹配的元素。

编辑:我修改了正则表达式以涵盖@Staffan Nöteberg 评论中提供的示例


0
投票

改进了处理其他场景的模式:

(?<=\btitle\s*=\s*['""])[^'""]*

© www.soinside.com 2019 - 2024. All rights reserved.