正则表达式以获取字符串的一部分 [英] Regular Expression to take part of a string

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

问题描述

大家好,



我有一个字符串,它是来自网站的页面源。

我需要一个正则表达式来从页面源中获取一些新闻。

该网站没有RSS,所以我不得不这样做。



我认为它会是这样的:

Hello all,

I have a string, it is a page source from a website.
I need a regular expression to get out some news items from the page source.
The website didn''t have RSS, so I''m having to do it this way.

I think it''ll be something like this:

"(?<=(<div id=""newsItem"">)).*?(?=(</div>))"



但是我对正则表达式非常了解,我一直都很喜欢它们。



任何人都可以帮忙解决这个问题吗?



非常感谢任何回复,

Tom。


But I''m very knew to regular expressions, I''ve always steered away from them until now.

Can anyone help with this issue please?

Any replies are greatly appreciated,
Tom.

推荐答案

获取 Expresso 的副本[ ^ ]并开始编写和测试表达式。没有比你需要的时候更好的学习时间了!
Get a copy of Expresso[^] and start writing and testing expressions. There''s no better time to learn than when you need it!






你的表达是正确的。使用匹配从HTML标记中获取新闻。首先,将其添加到代码文件的顶部:
Hi,

Your expression is correct. Use a Match to get the news from the HTML tags. First, add this at the top of your code file:
Imports System.Text.RegularExpressions



然后,使用此代码从HTML标签获取新闻:


Then, use this code to get the news from the HTML tags:

Dim newsAndHtmlTags As String = "<p><div id=""newsItem"">This is news!</div></p>"
Dim pattern As String = "(?<=(<div id=""newsItem"">)).*?(?=(</div>))"
Dim match As System.Text.RegularExpressions.Match = Regex.Match(newsAndHtmlTags, pattern)
Dim news As String = match.Value



希望这有帮助。


Hope this helps.


这篇关于正则表达式以获取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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