如何捕获< tags>内的文字,但从结果中排除标签? [英] How to capture text inside <tags> , but by excluding the tags from result?

查看:81
本文介绍了如何捕获< tags>内的文字,但从结果中排除标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Basic中我试图在标签之间获取文本,但也会捕获TAGS。



我希望在消息中看到结果仅为捕获的文本盒子。



但结果是以捕获文本的形式出现前后两个标签。







我的代码出了什么问题?



我尝试了什么:



Public Sub Button1_Click(发送者为对象,e为EventArgs)处理Button1.Click



Dim pattern As String =< tag>(。+?)< \ / tag>



Dim text As String =Other Text< tag>捕获文本其他文本



昏暗捕获匹配= Regex.Match(文本,模式)



MsgBox(capture.Value)



结束Sub

解决方案

正则表达式捕获您告诉它的文本 - 在您的情况下包括标签。

您可以使用非捕获前缀和后缀:

(?< =< tag>)(。+?)(?=< \ / tag>)

但是如果您正在尝试处理HTML,我建议使用正确的解析器: Html敏捷包| HAP [ ^ ]是一个好的开始。


我找到了解决方案



我必须使用Groups(1)才能获取标签内的文本..这是溶液..

In Visual Basic I am trying to get text between tags but TAGS are also captured.

I want to see the result as ONLY "Captured Text" in message box.

But result is coming as "Captured Text" with both tags before and after .



What is wrong in my code?

What I have tried:

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim pattern As String = "<tag>(.+?)<\/tag>"

Dim text As String = "Other Text <tag>Captured Text Other Text"

Dim capture As Match = Regex.Match(text, pattern)

MsgBox(capture.Value)

End Sub

解决方案

A regex captures the text you told it to - which in your case includes teh tags.
You can use an non-capturing prefix and suffix:

(?<=<tag>)(.+?)(?=<\/tag>)

But if you are trying to process HTML, I'd recommend a proper parser instead: Html Agility Pack | HAP[^] is a good start.


I found the solution

I must use Groups(1) for getting only text inside tags ..This is the solution..


这篇关于如何捕获&lt; tags&gt;内的文字,但从结果中排除标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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