开发者

Using Regex to alter a string in C# (based on ActionScript function)

开发者 https://www.devze.com 2023-01-23 18:30 出处:网络
I have some ActionScript code that splits a string using Regular Expression and lets me add content at the split location.

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开发者_StackOverflow 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>"


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));
}


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);


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;

    }


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消