正则表达式来提取属性值 [英] Regex to extract attribute value

查看:150
本文介绍了正则表达式来提取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ... $是一种快速提取HTML表格标题属性值的方法b $ b< li>< a href =/ wiki / Proclotitle =Proclo> Proclo< / a>< / li> 
< li>< a href =/ wiki / Proclustitle =Proclus> Proclus< / a>< / li>
< li>< a href =/ wiki / Ptolemytitle =托勒密>托勒密< / a>< / li>
< a href =/ wiki / Pythagorastitle =Pythagoras> Pythagoras< / a>< / li>< / ul>< h3> S< / h3>
...

所以它会返回Proclo,Proclus,Ptolemy,Pythagoras,.. ..在每行的字符串中。我正在使用StreamReader读取文件。我使用C#。



谢谢。

解决方案

C#正则表达式将查找所有标题值:

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

C#代码如下所示:

<$ p $正则表达式regex = new Regex(@(?< = \btitle =)[^] *);
匹配匹配= regex.Match(输入);
string title = match.Value;

正则表达式使用正向lookbehind 来查找 title 值开始。然后它将所有内容匹配到结尾双引号。


What would be a quick way to extract the value of the title attributes for an HTML table:

...
<li><a href="/wiki/Proclo" title="Proclo">Proclo</a></li>
<li><a href="/wiki/Proclus" title="Proclus">Proclus</a></li>
<li><a href="/wiki/Ptolemy" title="Ptolemy">Ptolemy</a></li>
<li><a href="/wiki/Pythagoras" title="Pythagoras">Pythagoras</a></li></ul><h3>S</h3>
...

so it would return Proclo, Proclus, Ptolemy, Pythagoras,.... in strings for each line. I'm reading the file using a StreamReader. I'm using C#.

Thank you.

解决方案

This C# regex will find all title values:

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

The C# code is like this:

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

The regex uses positive lookbehind to find the position where the title value starts. It then matches everything up to the ending double quote.

这篇关于正则表达式来提取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆