Home > Software design >  Vb.net find and replace within paragraph
Vb.net find and replace within paragraph

Time:04-19

What would be the efficient way to read a paragraph, and replace anything between square brackets []? In my case following paragraph,

I agree to [Terms of Use|https://www.google.com/terms-use], [Privacy Statement|https://www.google.com/privacy-statement] and [Misc Term|https://www.google.com/misc-terms]

should parsed as,

I agree to Terms of Use, Privacy Statement and Misc Term

CodePudding user response:

Dim rtnStr = "String goes here"
Dim pattern As String = "\[.*?\]"

If Regex.IsMatch(rtnStr, pattern) Then
                        Dim matches = Regex.Matches(rtnStr, pattern, RegexOptions.IgnoreCase)
                        For index As Integer = 0 To matches.Count - 1
                            If matches(index).ToString().Contains("|") Then
                                Dim splitBracket = matches(index).ToString().Split("|")
                                Dim linkName = String.Empty
                                Dim linkUrl = String.Empty
                                If splitBracket.Length > 0 Then
                                    linkName = splitBracket(0).Replace("[", "")
                                    linkUrl = splitBracket(1).Replace("]", "")
                                End If
                                Dim linkHtml = "<a terms"" href=""javascript: void(0);"" data-url="   linkUrl   ">"   linkName   "</a>"
                                rtnStr = rtnStr.Replace(matches(index).ToString(), linkHtml)
                            End If
                        Next
                    End If
                    @Html.Raw(rtnStr)

CodePudding user response:

You can use the following regular expression pattern to match the brackets:

\[[^]] ]

This is what the pattern means:

  • \[ match an open bracket
  • [^]] match anything but a closed bracket one or more times
  • ] match a closed bracket

Here is an example fiddle: https://regex101.com/r/Iyk9kR/1

Once you have the matches, you would:

  1. Use Regex.Replace (documentation)
  2. Use the MatchEvaluator overload
  3. In the MatchEvaluator, use String.Split (documentation) on the pipe character
  4. Dynamically build your anchor tag by setting the href attribute to the second match of the split and the innerHTML to the first match of the split

It might be worth adding some conditional checking in between steps 2 and 3, but I'm not sure of your exact requirements.

Here is an example:

Imports System
Imports System.Text.RegularExpressions
Public Module Module1
    Public Sub Main()
        Dim literal = "I agree to [Terms of Use|https://www.google.com/terms-use], [Privacy Statement|https://www.google.com/privacy-statement] and [Misc Term|https://www.google.com/misc-terms]"
        Dim regexPattern = "\[[^]] ]"
        Dim massagedLiteral = Regex.Replace(literal, regexPattern, AddressOf ConvertToAnchor)
        Console.WriteLine(massagedLiteral)
    End Sub
    
    Private Function ConvertToAnchor(m As Match) As String
        Dim matches = m.Value.Split("|"c)
        Return "<a href=""" & matches(1).Substring(0, matches(1).Length - 1) & """>" & matches(0).Substring(1) & "</a>"
    End Function
End Module

Fiddle: https://dotnetfiddle.net/xUE8St

  • Related