2010-11-02 2 views
1

J'ai du code ActionScript qui sépare une chaîne en utilisant Regular Expression et me permet d'ajouter du contenu à l'emplacement de la division.Utilisation de Regex pour modifier une chaîne en C# (basé sur la fonction 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; 
} 

J'essaie de trouver le C# équivalent à la fonction ci-dessus

Ce que cela fait est de vous donner la valeur entière de l'insertion de texte du texte de remplacement où l'expression régulière trouve une correspondance.

Par exemple:

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

se transformerait en

var newString:String = "hello there <font color="#666666"> <a href="http://www.url.com" target="_blank">http://www.url.com</a></font>" 
+1

Avez-vous essayé la classe Regex en C#? –

Répondre

1

En utilisant une combinaison des réponses, voici la fonction en C# qui fait la même chose que la fonction 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; 
     } 

    } 

Le code ci-dessus ne fonctionne que sur le premier match, si vous avez plusieurs correspondances, vous devez utiliser ce 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; 

    } 
0

Comme cela?

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