2 VB 正则表达式问题
我需要一些有关 VB RegEx 的帮助。
我有两个正则表达式,我需要做两件特定的事情。
RegEx 一 - 我不太确定如何执行此操作,但我需要在 Href 标记中获取所有内容。即
String = "<a href=""test.html"">"
我需要正则表达式返回... test.html
正则表达式二 - 我已经部分完成了这项工作。
我有这样的标签,
RegEx = "<div class=""top""(.*?)</div>"
String = "<div class=""top""><a><b><div class=""bottom""></div></b></a></div>"
我遇到的问题是这不返回任何内容,它应该返回带有“top”的所有内容,但它什么也不返回。
I need some help with a VB RegEx.
I've got two RegEx that I need to do two specific things.
RegEx one - I am not exactly sure how to do this, but I need to get everything within a Href tag. i.e.
String = "<a href=""test.html"">"
I need the RegEx to return .... test.html
RegEx Two - I have partly got this working.
I've got tags like
RegEx = "<div class=""top""(.*?)</div>"
String = "<div class=""top""><a><b><div class=""bottom""></div></b></a></div>"
The problem I have is this isnt returning anything, it should return everything withing "top", but it returns nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两种用例都不能用正则表达式很好地解决。
请改用 HTML 解析器,例如 HTML Agility Pack。
Neither use-case can be solved well with regular expressions.
Use an HTML parser instead, e.g. the HTML Agility Pack.
好吧,如果您的 html 不包含嵌套标签,您可以使用正则表达式完成第一部分(只要您可以控制搜索源代码,您就可以更加确定您的结果)。
test.html 将在称为
$1
的非被动组中找到。第二部分我担心你在那里嵌套了标签并且它在这方面失败了。正则表达式和 html 的问题在于,正则表达式无法深入研究嵌套允许但不是最佳实践的代码,这些代码可以按预期执行但格式不正确。
您可以发布第二个案例的一些搜索源以便我们查看吗?
Well, if your html doesn't contain nested tags you can do the first part with regex (as long as you can control your search source code, you can be much more certain of your results).
the test.html will be found in the non-passive group referred to as
$1
.The second part I'm concerned that you have nested tags in there and it's failing on that. The thing with regex and html is that regex can't delve well into the nested-allowable-but-not-best-practice code that can execute as expected but isn't well formed.
Can you post some search source for the second case so we can look?