Regex to replace all matches in string with link within richtextbox

stevenryals

Member
Joined
May 21, 2014
Messages
5
Programming Experience
Beginner
I have been beating myself up over this, I can not seem to get this to work.

I am passing a string into a function to parse the string.
1) i am replacing all "|" with vbCrLf
2) there are instances of UserNames and I need to replace them with "internallink://action/username"
(this will trigger a local program to link to a user within the system)

This is a windows form application and is displaying notes.
I've tried about 50 different iterations of this, but here is my current code.

VB.NET:
    Public Sub ParseNotesHelper(ByRef PassedNotes As String)
        '   separate notes into individual lines separated by line break
        PassedNotes = PassedNotes.Replace("|", vbCrLf & vbCrLf)
        '   add link to each user id within passed note string
        Dim nameregex As Regex = New Regex("[A-Z]* [A-Z]*: ", RegexOptions.IgnoreCase)
        ' regex to grab text within any parentheses
        Dim uidregex As Regex = New Regex("\((.*?)\)", RegexOptions.IgnoreCase)
        ' regex to be more specific to the setup of the user name/user id
        Dim uidregex2 As Regex = New Regex("((?:[a-z][a-z]*[0-9]+[a-z0-9]*))", RegexOptions.IgnoreCase)
        Dim i As Integer = 0


        For Each user As Match In uidregex2.Matches(PassedNotes)
            'Dim linklbl As New LinkLabel
            Dim tempid As String = user.ToString()
            Dim userid As String
            Dim linkstring As String
            userid = tempid.Trim("("c, ")"c)
            'linklbl.Text = userid
            linkstring = "internalApplication://action/" + userid
            'Dim indexstart As Integer = user.Index
            'Dim indexend As Integer = indexstart + 6
            'Dim link As LinkLabel.Link = linklbl.Links.Add(indexstart, indexend, linkstring)
            Regex.Replace(user.Value, "((?:[a-z][a-z]*[0-9]+[a-z0-9]*))", linkstring)
            i = i + 1
        Next

    End Sub

I have had absolutely zero luck with adding this link.. (currently as you see I have some lines commented out, but I've defo tried them in many iterations)

Any thoughts or direction would be extremely appreciated..

Thanks
 
UPDATE: To make the replace easier I have changed to a webform so that we can treat it as a normal link.
So what I have now is: (only within the for each loop)

VB.NET:
For Each user As Match In uidregex.Matches(PassedNotes)
            Dim userid As String = user.ToString
            Dim linkstring As String = "<a href='qto://talk/" + userid + "'>" + userid + "<a>"
            Dim s As String = user.ToString.Replace(userid, linkstring)
Next

I have tested the regex, and it is returning exactly what I want it to.
For some reason the PassedNotes string isn't getting updated at all..

Any help greatly appreciated.
 
Hi,

The reason why the PassedNotes string is not updated is because you are NOT applying any updates to that string. All you are doing at the moment is iterating the MatchCollection returned by RegEx and then creating a variable called "s" which is a replacement of the UserID with your Link String.

What you need to do is iterate the MatchCollection and then Replace that Match with your Link string in the PassedNotes variable.

Hope that helps.

Cheers,

Ian
 
Thats what i'm "trying" to do..

the "user" match iteration is not something I can update as 'string can not be converted to type match"

how would I update only the "user" match element within "PassedNotes" string ?
 
Thanks for the pointer..

in the end, since I had multiple instances of the same substring i had to dim an arraylist to store uids and check against..

VB.NET:
     If Not useridlist.Contains(userid) Then
                useridlist.Add(userid)
                PassedNotes = PassedNotes.Replace(userid, linkstring)
            End If


Thanks again.. it's always the simple stuff that i overlook lol
 
Back
Top