Click here to Skip to main content
14,828,250 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I am try to find href tags from string. Then i try to find is particular words is matched in href and replace using VB.Net Regex

Public arrString() As String = New String() {"-bigsmile-", "-sadsmile-", "-worried-", "-cool-", "-wink-", "-sleepy-", "-blush-", "-wondering-", "-talking-", "-angry-", "-smile-", "-calls-", "-yesicon-", "-noicon-"}

Dim inputString as string = "<a href=></a>"

'Note: -bigsmile- is in arrString(). My aim is to replace every words from arrString contains in inputString
'i want the result should be <a href=></a>
' -bigsmile- is removed from href

Dim new_inputString as string = DumpHRefs(inputString)

Function DumpHRefs(ByVal inputString As String) As String
    Dim HRefPattern As String = "href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))"
    Dim m As Match = Regex.Match(inputString, HRefPattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)

    Do While m.Success
      'here i found and i dont know how to replace / remove -bigsmile-
      Console.WriteLine("Found href {0} at {1}.", m.Groups(1), m.Groups(1).Index)
      m = m.NextMatch()

    Return inputString 
End Function

Regexes are clever beasts, one opf the overloads for Rexex.Replace uses a match evaluator delegate:[^] which allows you to do further processing on the text matched by the Regex.
If you write your routine to compare the match text as supplied by the Match parameter text, you can replace only those strings you are interested in. The link has an example.
I think this article could be of some help.

Find and Replace with Regular Expressions[^]
Thanks OriginalGriff, for guide me to right way

i have done it myself

Function DumpHRefs(ByVal inputString As String) As String
    Dim HRefPattern As String = "href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))"
    Dim r3 As New Regex(HRefPattern, RegexOptions.IgnoreCase)
    inputString = r3.Replace(inputString, AddressOf smily_replace)

    Return inputString
  End Function

  Function smily_replace(ByVal newstr As Match) As String
    Dim x As String = newstr.ToString()

    For Each word In arrString
      If x.Contains(word) Then x = x.Replace(word, "")

    Return x
  End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900