This project has moved and is read-only. For the latest updates, please go here.

Extension method to embed tweets only works on the first match?

Jul 24, 2014 at 10:07 PM
I have the following extension method that is supposed to convert [tweet:statusid] into an embedded tweet using Linq To Twitter. However only the first shortcode is converted to an embedded tweet. Any others just show [tweet:statusid]. The same chunk of code works perfectly in a different application except it is in c#. Using Linq to Twitter 2.1.7. Is there anything wrong with the method?
<Extension> Public Function ConvertTweetShortCode(text As String)
        Dim auth = New SingleUserAuthorizer() With { _
        .Credentials = New InMemoryCredentials() With { _
        .ConsumerKey = "KeyGoesHere", _
        .ConsumerSecret = "Secret", _
        .OAuthToken = "Token", _
        .AccessToken = "AccessToken"}}
        Dim ctxTwitterContext = New TwitterContext(auth)
        Dim regex__1 As String = "\[tweet:.*?\]"            
        Dim matches As MatchCollection = Regex.Matches(text, regex__1)
        If matches.Count = 0 Then
            Return text
        End If
        Dim TweetID As String
        For i As Integer = 0 To matches.Count - 1
            Dim length As Int32 = "[tweet:".Length
            TweetID = matches(i).Value.Substring(length, matches(i).Value.Length - length - 1)
            Dim embed = (From tweet In ctxTwitterContext.Status Where tweet.Type = StatusType.Oembed AndAlso tweet.ID = TweetID Select tweet.EmbeddedStatus).First
            Dim html As String = embed.Html
            Return text.Replace(matches(i).Value, String.Format(html))
    End Function
Jul 31, 2014 at 5:36 AM

Set a breakpoint and look at the value of embed.Html to verify that it's returning a valid value. Also, step through the code, looking at the value of the text parameter, the TweetID from the substring of matches, and what happens when the regex executes for both Matches and Replace.