替换为正则表达式

编程入门 行业动态 更新时间:2024-10-09 13:21:43
本文介绍了替换为正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 我正在尝试使用正则表达式替换一些文本。我不确定它的相关性,但文本来自vb中的Treeview控件。我使用以下基本正则表达式:< creationDate>(。*?)< / creationDate>哪个很棒! 这里是子: Dim nNode As TreeNode Dim sPattern As String ="<" &安培; sTag& "><(*); /" &安培; sTag& ">" Dim rx As New Regex(sPattern) Dim m As Match 每个nNode在nParent.Nodes中 m = rx.Match(nNode.Text) 如果m.Success = True那么 rx。替换(nNode.Text,sPattern,sText) 退出Sub 结束如果 下一个 所以正则表达式找到我正在寻找的模式。但替换不起作用。我注意到rx.Replace函数返回一个字符串。当我从rx.Replace返回一个值时 - 它只是sText值。我希望能够保留标签值。我知道我可以使用蛮力方法解决这个问题,但我希望使用正则表达式的优雅。 谢谢!

解决方案

你可以做的就是将sPattern字符串修改为:Dim sPattern As String ="(<"& sTag&" ;>)(。*?)(< /"& sTag&">)" 从那里你可以像这样修改sText值:"

1" + sText +"

3" 有关此方法的更多信息,请访问: www.javascriptkit/javatutors/re3.shtml 结果

Hi! I am trying to replace a bit of text using a regex.  Im not sure its relevant but the text is from a Treeview control in vb.  I am using the following basic regex: <creationDate>(.*?)</creationDate> which works great! here is the sub: Dim nNode As TreeNode         Dim sPattern As String = "<" & sTag & ">(.*?)</" & sTag & ">"         Dim rx As New Regex(sPattern)         Dim m As Match         For Each nNode In nParent.Nodes             m = rx.Match(nNode.Text)             If m.Success = True Then                 rx.Replace(nNode.Text, sPattern, sText)                 Exit Sub             End If         Next So the regex works as far as finding the pattern Im looking for.  But the replace isn't working.  I noticed that the rx.Replace function returns a string.  When I return a value from the rx.Replace -  its the sText value only.  I want to be able to keep the tag values.  I know I can solve this using a brute force method, but I was hoping to use the regex elegance.thanks!

解决方案

On thing you could do is modify the sPattern string to: Dim sPattern As String = "(<" & sTag & ">)(.*?)(</" & sTag & ">)"From there you can  modify the sText value like so: "

1"+sText+"

3"More information on this method can be found here: www.javascriptkit/javatutors/re3.shtml

更多推荐

替换为正则表达式

本文发布于:2023-06-11 03:30:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/625721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!