Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式更改C#中的字符串(基于ActionScript函数)_C#_Actionscript 3 - Fatal编程技术网

使用正则表达式更改C#中的字符串(基于ActionScript函数)

使用正则表达式更改C#中的字符串(基于ActionScript函数),c#,actionscript-3,C#,Actionscript 3,我有一些ActionScript代码,它使用正则表达式拆分字符串,并允许我在拆分位置添加内容 // AS3 Code function formatTweetText(tweet:String ):String { var regUrl:RegExp = /http:\/\/\S+/g; var _text:String = " "+ tweet + " "; _text = _tex

我有一些ActionScript代码,它使用正则表达式拆分字符串,并允许我在拆分位置添加内容

// 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;
}
会变成

var newString:String =  "hello there <font color="#666666"> <a href="http://www.url.com" target="_blank">http://www.url.com</a></font>"
var newString:String=“你好”
像这样吗

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;
}
字符串格式tweettext(字符串tweet)
{
string regUrl=“http:\/\/\S+”;
字符串文本=“tweet+”;
text=Regex.Replace(文本、规则、,
" ");
text=text.Substring(1,text.Length-2);
返回文本;
}
像这样吗

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;
}
字符串格式tweettext(字符串tweet)
{
string regUrl=“http:\/\/\S+”;
字符串文本=“tweet+”;
text=Regex.Replace(文本、规则、,
" ");
text=text.Substring(1,text.Length-2);
返回文本;
}
Regex regUrl=new Regex(@“http://\S+”);
字符串url=regUrl.Match(tweet.Value);
返回字符串。格式(“,url);
Regex regUrl=new Regex(@“http://\S+”);
字符串url=regUrl.Match(tweet.Value);
返回字符串。格式(“,url);

使用这些答案的组合,这里是C#中的函数,其功能与ActionScript函数相同

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

    }
私有字符串格式tweettext(字符串tweet)
{
Regex regUrl=新的Regex(@“http://\S+”);
字符串url=regUrl.Match(tweet.Value);
如果(url.Length>0)
{
string newReturnVal=string.Format(“{0}”,url);
字符串returnVal=tweet.Replace(url,newReturnVal);
返回值;
}
其他的
{
返回tweet;
}
}
上述代码仅适用于第一个匹配项,如果有多个匹配项,则需要使用以下代码:

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;

    }
私有字符串格式tweettext(字符串tweet)
{
字符串returnVal=tweet;
字符串updatedValue=tweet;
Regex regUrl=新的Regex(@“http://\S+”);
Match matches=regUrl.Match(tweet);
while(匹配成功)
{
对于(int i=0;i 0)
{
string newReturnVal=string.Format(“{0}”,url);
returnVal=updatedValue.Replace(url,newReturnVal);
}
updatedValue=returnVal;
}   
}
matches=matches.NextMatch();
}
返回值;
}

使用这些答案的组合,这里是C#中的函数,其功能与ActionScript函数相同

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

    }
私有字符串格式tweettext(字符串tweet)
{
Regex regUrl=新的Regex(@“http://\S+”);
字符串url=regUrl.Match(tweet.Value);
如果(url.Length>0)
{
string newReturnVal=string.Format(“{0}”,url);
字符串returnVal=tweet.Replace(url,newReturnVal);
返回值;
}
其他的
{
返回tweet;
}
}
上述代码仅适用于第一个匹配项,如果有多个匹配项,则需要使用以下代码:

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;

    }
私有字符串格式tweettext(字符串tweet)
{
字符串returnVal=tweet;
字符串updatedValue=tweet;
Regex regUrl=新的Regex(@“http://\S+”);
Match matches=regUrl.Match(tweet);
while(匹配成功)
{
对于(int i=0;i 0)
{
string newReturnVal=string.Format(“{0}”,url);
returnVal=updatedValue.Replace(url,newReturnVal);
}
updatedValue=returnVal;
}   
}
matches=matches.NextMatch();
}
返回值;
}

你试过c#中的正则表达式类吗?你试过c#中的正则表达式类吗?