Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET RegEx
Hi,
 
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=http://www.domain.com-bigsmile->http://www.domain.com-bigsmile-</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=http://www.domain.com>http://www.domain.com-bigsmile-</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 http://www.domain.com-bigsmile- 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()
    Loop
 
    Return inputString 
End Function
Posted 5-Jul-12 20:37pm
kals84713
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Regexes are clever beasts, one opf the overloads for Rexex.Replace uses a match evaluator delegate: http://msdn.microsoft.com/en-us/library/cft8645c.aspx#Y0[^] 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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think this article could be of some help.
 
Find and Replace with Regular Expressions[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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, "")
    Next
 
    Return x
  End Function
  Permalink  

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

  Print Answers RSS
0 Maciej Los 470
1 Sergey Alexandrovich Kryukov 424
2 OriginalGriff 360
3 Abhinav S 250
4 CHill60 240
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 6 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100