简体   繁体   English

匹配“ THIS”并替换为“ THAT” RegEx Vb.Net

[英]Match “THIS” And Replace with “THAT” RegEx Vb.Net

Trying to find out how to find and replace text with corresponding values. 试图找出如何查找和替换具有相应值的文本。

For Example 例如

1) fedex to FedEx 2) nasa to NASA 3) po box to PO BOX 1)将联邦快递(Fedex)运送至FedEx(联邦快递)2)将美国国家航空航天局(NASA)运送至美国国家航空航天局(NASA)3)将邮政信箱发送至邮政信箱

Public Function FindReplace(ByVal s As String) As String

    Dim MatchEval As New MatchEvaluator(AddressOf RegexReplace)

    Dim Pattern As String = "(?<f1>fedex|nasa|po box)"

    Return Regex.Replace(s, Pattern, MatchEval, RegexOptions.IgnoreCase)

End Function

Public Function RegexReplace(ByVal m As Match) As String
    Select Case LCase(m.Groups("f1").Value)
        Case "fedex"
            Return "FedEx"
        Case "nasa"
            Return "NASA"
        Case "po box"
            Return "PO BOX"
    End Select
End Function

The above code is working fine for fixed values but don't know how to use the above code to match added values on run-time like db to Db. 上面的代码适用于固定值,但不知道如何使用上面的代码在运行时(如db到Db)匹配添加的值。

I'd guess, that the only thing here you need Regex for is IgnoreCase option. 我猜想,这里唯一需要Regex的是IgnoreCase选项。 If so, then I would like to suggest not to use Regex at all. 如果是这样,那么我建议完全不要使用Regex。 Use String functionality instead: 改用String功能:

Dim input As String = "fEDeX"
Dim pattern As String = "fedex"
Dim replacement As String = "FedEx"

Dim result As String

result = input.ToLowerInvariant().Replace(pattern, replacement)

But if you still need Regex, then this should work: 但是,如果您仍然需要Regex,则应该可以使用:

result = Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase)

Example: 例:

Sub Main()
  Dim replacements As New Dictionary(Of String, String)()
  replacements.Add("fedex", "FedEx")
  replacements.Add("nasa", "NASA")
  replacements.Add("po box", "PO BOX")

  Dim result As String = Replace("fedex, nAsA, po box, etc", replacements)
End Sub

Private Function Replace(ByVal input As String, ByVal replacements As Dictionary(Of String, String)) As String
  For Each item In replacements
    input = Regex.Replace(input, item.Key, item.Value, RegexOptions.IgnoreCase)
  Next

  Return input
End Function

Found the solution by using List and did the performance test against dictionary object suggested by Anton Kedrov both methods takes almost same time to complete but i don't know the dictionary method will be good or not for longer replacement list because it loop through all the list to find the match entry for replacement. 通过使用List找到了解决方案,并对Anton Kedrov建议的字典对象进行了性能测试,两种方法几乎都需要花费相同的时间才能完成,但是我不知道字典方法是否适合较长的替换列表,因为它遍历所有列表中找到要替换的匹配项。

I thank you all for your suggestion and advice. 我感谢大家的建议。

Sub Main()
    Dim lst As New List(Of String)

    lst.Add("NASA")
    lst.Add("FedEx")
    lst.Add("PO BOX")

    MsgBox(FindReplace("this is testing fedex naSa PO box"))
End Sub

Public Function FindReplace(ByVal s As String) As String
    Dim Pattern As String = "(?<f1>fedex|nasa|po box)"
    Dim MatchEval As New MatchEvaluator(AddressOf RegexReplace)
    Return Regex.Replace(s, Pattern, MatchEval, RegexOptions.IgnoreCase)

End Function

Public Function RegexReplace(ByVal m As Match) As String
    Dim Found As String
    Found = lst.Find(Function(value As String) LCase(value) = LCase(m.Groups("f1").Value))
    Return Found
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM