如何用 vb.net 替换此字符串的某些部分? [英] How to replace some part of this string with vb.net?

查看:36
本文介绍了如何用 vb.net 替换此字符串的某些部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求创建正则表达式的帮助,以便我可以用锚标记替换文本.文本来自 SQL 字段 (VarChar(max)) 并且格式如下:

Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. 98)

Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua (1998, cc. 553, 568; 2001, c. 300)

在上面的文本中,我需要将1994年之后的所有章节都替换为锚标签.例如,98、553、568 和 300 都将被替换.以下代码查找 1994 年的整个文本,例如 c.98,但我不确定如何仅替换该文本中的98".

公共共享函数 ReplaceChapterTag1(lang As String) As StringDim l As String = langDim r As Regex = New Regex("199[4-9][/,][/][/c]*[/.][/][0-9]+(?:\.[0-9]*)?")Dim applyEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf applyCodeLink)l = r.Replace(l, applyEvaluator)返回 l结束函数私有共享函数 applyCodeLink(ByVal m As Match) As StringDim r As Regex = New Regex("^[0-9]*[\-][0-9]*")Dim str As String = m.ToStringDim strReturn As String = ""Dim match As Match = r.Match(str)如果 match.Success 那么strReturn = str别的strReturn = "<a href='link?id=" &m.价值&"'>"&m.价值&</a>"万一返回 strReturn结束函数

解决方案

解决方案

<块引用>

我不确定如何只替换该文本中的98".

您可以使用Regex.Replace.但是,您构建的正则表达式需要像这样调整:

(?<=199[4-9][^;]+)(?<=[/c]*[/.][/\x20]|,\x20)(\d+(?:\.\d*)?)(?=[,;)])

说明

示例代码

' 输入Dim InputText As String = "..." ' Lorem ipsum...' 正则表达式Dim r As Regex = New Regex( _"(?<=199[4-9][^;]+)" + _"(?<=[/c]*[/.][/\x20]|,\x20)" + _"(\d+(?:\.\d*)?)" + _"(?=[,;)])", _RegexOptions.IgnoreCase _或 RegexOptions.CultureInvariant _或 RegexOptions.Compiled _)' 这是替换字符串Dim Replacement As String = "<a href='link?id=$1'>$1</a>"'' 使用替换模式替换 InputText 中匹配的文本Dim Result As String = r.Replace(InputText,Replacement)

输入

<块引用>

Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut laboure et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. 98)

Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua (1998, cc. 553, 568; 2001, cc. 17, 300)

输出

Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. <a href='link?id=98'>98</a>)Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua (1998, cc. <a href='link?id=553'>553</a>, <a href='link?id=568'>568; 2001, cc. 17, 300)

讨论

基本上,我的答案中调整后的正则表达式背后的想法是寻找一个或多个数字 (\d+) 前面和后面是一些字符.

我冒昧地简化并使初始正则表达式更清晰.主要是我替换了:

  • [0-9]\d
  • (space char)\x20

I am looking for help creating a regular expression so that I can replace text with an anchor tag. The text is coming from a SQL field (VarChar(max)) and is formatted so:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. 98)

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1998, cc. 553, 568; 2001, c. 300)

In the above text, I need to replace all chapters after 1994 with anchor tags. So for example, 98, 553, 568 and 300 would all be replaced. The following code finds the entire text of 1994, c.98 for example, but I'm not sure how I would replace just the "98" in that text.

Public Shared Function ReplaceChapterTag1(lang As String) As String
    Dim l As String = lang
    Dim r As Regex = New Regex("199[4-9][/,][/ ][/c]*[/.][/ ][0-9]+(?:\.[0-9]*)?")

    Dim applyEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf applyCodeLink)
    l = r.Replace(l, applyEvaluator)

    Return l

End Function

Private Shared Function applyCodeLink(ByVal m As Match) As String
    Dim r As Regex = New Regex("^[0-9]*[\-][0-9]*")
    Dim str As String = m.ToString
    Dim strReturn As String = ""

    Dim match As Match = r.Match(str)
    If match.Success Then
        strReturn = str
    Else
        strReturn = "<a href='link?id=" & m.Value & "'>" & m.Value & "</a>"
    End If

    Return strReturn
End Function

解决方案

Solution

I'm not sure how I would replace just the "98" in that text.

You can use Regex.Replace. However, the regex you have built need to be tuned like this:

(?<=199[4-9][^;]+)(?<=[/c]*[/.][/\x20]|,\x20)(\d+(?:\.\d*)?)(?=[,;)])

Description

Sample code

' Input
Dim InputText As String = "..." ' Lorem ipsum...

' Regex
Dim r As Regex = New Regex( _
      "(?<=199[4-9][^;]+)" + _
      "(?<=[/c]*[/.][/\x20]|,\x20)" + _
      "(\d+(?:\.\d*)?)" + _
      "(?=[,;)])", _
    RegexOptions.IgnoreCase _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.Compiled _
    )

' This is the replacement string
Dim Replacement As String = "<a href='link?id=$1'>$1</a>"

'' Replace the matched text in the InputText using the replacement pattern
Dim Result As String = r.Replace(InputText,Replacement)

Input

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. 98)

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1998, cc. 553, 568; 2001, cc. 17, 300)

Output

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1954, c. 12; 1968, c. 300; 1994, c. <a href='link?id=98'>98</a>)

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua (1998, cc. <a href='link?id=553'>553</a>, <a href='link?id=568'>568</a>; 2001, cc. 17, 300)

Discussion

Basically, the idea behind the tuned regex in my answer is to look for one or more digit(s) (\d+) that are preceded AND followed by some characters.

I took the liberty to simplify and make clearer the initial regexp. Mainly, I replaced:

  • [0-9] with \d
  • (space char) with \x20

这篇关于如何用 vb.net 替换此字符串的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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