1

I have some ActionScript code that splits a string using Regular Expression and lets me add content at the split location.

// AS3 Code
function formatTweetText(tweet:String ):String
    {
        var regUrl:RegExp = /http:\/\/\S+/g;
        var _text:String = " "+ tweet + " ";                            
        _text = _text.replace(regUrl, '<font color="#666666"> <a href="$&" target="_blank">$&</a></font>');
        _text = _text.substr(1, _text.length-2);
            return _text;
}

I'm trying to find the C# equivalent to the above function

What this does is give you the entire text value inserting the Replacement text where the regular expression finds a match.

For example:

var text:String = "Hello there http://www.url.com";

would turn into

var newString:String =  "hello there <font color="#666666"> <a href="http://www.url.com" target="_blank">http://www.url.com</a></font>"
1
  • 1
    Have you tried the Regex class in c#? Commented Nov 2, 2010 at 18:30

4 Answers 4

2
using System.Text.RegularExpressions;

private string formatTweetText(string tweet)
{
    string newText = " " + Regex.Replace(tweet, "/http:\/\/\S+/g", "Replacement String") + " ";
    return newText.SubString(1, (newText.Length - 2));
}
Sign up to request clarification or add additional context in comments.

Comments

1
Regex regUrl = new Regex(@"http://\S+");
string url = regUrl.Match(tweet).Value;
return string.Format("<font color=\"#666666\"> <a href=\"{0}\" target=\"_blank\">{0}</a></font>", url);

Comments

1

Using a combination of the answers, here is the function in C# that does the same thing as the ActionScript function.

private string formatTweetText(string tweet)
    {

        Regex regUrl = new Regex(@"http://\S+");
        string url = regUrl.Match(tweet).Value;
        if (url.Length > 0)
        {
            string newReturnVal = string.Format("<font color=\"#FF0000\">{0}</font>", url);
            string returnVal =  tweet.Replace(url, newReturnVal);
            return returnVal;
        }
        else
        {
            return tweet;
        }

    }

The above code only works on the first match, if you have multiple matches you'll need to use this code:

private string formatTweetText(string tweet)
    {

        string returnVal = tweet;
        string updatedValue = tweet;
        Regex regUrl = new Regex(@"http://\S+");
        Match matches = regUrl.Match(tweet);
        while (matches.Success)
        {
            for (int i = 0; i <= 2; i++)
            {
                Group g = matches.Groups[i];
                CaptureCollection cc = g.Captures;
                for (int j = 0; j < cc.Count; j++)
                {
                    Capture c = cc[j];
                    string url = c.Value;
                    if (c.Length > 0)
                    {
                        string newReturnVal = string.Format("<font color=\"#FF0000\">{0}</font>", url);
                        returnVal = updatedValue.Replace(url, newReturnVal);

                    }
                    updatedValue = returnVal;
                }   

            }
            matches = matches.NextMatch();
        }
        return returnVal;

    }

Comments

0

Like this?

string FormatTweetText(string tweet)
{
   string regUrl = "http:\/\/\S+";
   string text = " " + tweet + " ";
   text = Regex.Replace(text, regUrl, 
      "<font color=\"#666666\"> <a href=\"$&\" target=\"_blank\">$&</a></font>");

   text = text.Substring(1, text.Length - 2);
   return text;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.