c# - Emoticon Replace Using Regex for Keys in a Dictionary -


i found elegant solution i'm trying execute, replacement of emotes in string url (it'll end being img tag once figure out. solution found here: iterating on dictionary of regex in c#. issue when reiterates on affected content block (output) replaces :/ in http:// emote url.

i getting emotes twitch api, provides regex each emote , url image.

i'd replace of emotes in dictionary without overwriting instances contained in urls. possible? thought simple effort it's ended spiraling way out of realm of knowledge @ point.

public static string replacements(string text) {      string output = text;     foreach (keyvaluepair<string, string> item in dict1)     {         //here replace output again         output = regex.replace(output, item.key, item.value);       }       return output; } 

edit: sorry didn't give enough info; still new posting here. regexes fetched twitch api streamer's emotes. here's example: twitch api. json string provides emotes, each emote containing regex identifier emote , url image of emote.

"emoticons":[     {"width":24,"height":18,"regex":"\\:-?d","state":"active","subscriber_only":false,"url":"http://static-cdn.jtvnw.net/jtv_user_pictures/chansub-global-emoticon-9f2ac5d4b53913d7-24x18.png"}     {"width":24,"height":18,"regex":"\\:-?[\\\\/]","state":"active","subscriber_only":false,"url":"http://static-cdn.jtvnw.net/jtv_user_pictures/chansub-global-emoticon-374120835234cb29-24x18.png"} ...] 

i'm pulling out regex , url image dictionary; key regex , value url. using code provided above, replaces :/ in http:// emote :/, after run code end following:

httphttp://static-cdn.jtvnw.net/jtv_user_pictures/chansub-global-emoticon-374120835234cb29-24x18.png/static-cdn.jtvnw.net/jtv_user_pictures/chansub-global-emoticon-9f2ac5d4b53913d7-24x18.png 

i hope clarifies predicament more.

because regex scans whole string on again don't think regex.replace solution fit here.

here alternative; posted method, better suggestions welcome :)

public string replacemethod(string input, dictionary<string,string> dict1 ) {     if (dict1.any(c => string.isnullorempty(c.key)))                 throw new argumentexception("dictionary may not contain empty key's");      stringbuilder output = new stringbuilder();      //loop string's characters     (int c = 0; c < input.length; c++)     {        bool found = false;        //order length desc ensure longest possible match checked first        foreach (keyvaluepair<string, string> item in                                       dict1.orderbydescending(x => x.key.length))        {            if (input.substring(c).startswith(item.key))            {                //match found                found = true;                //skip length of key                c+=item.key.length - 1;                output.append(item.value);                break;             }         }          if (!found)             output.append(input[c]);     }      return output.tostring(); } 

as console app:

using system; using system.collections.generic; using system.linq; using system.text;  namespace consoleapplication6 {     class program     {            public static string replacemethod(string input, //... above                                              dictionary<string,string> dict1)          static void main(string[] args)         {             var dict1 = new dictionary<string, string>();              dict1.add(":-)", "***http://smile.jpg***");             dict1.add(":/", "***http://hmmm.jpg***");             dict1.add(":(", "***http://sad.jpg***");             dict1.add("):(", "***http://strange? :)***");              string input = ":-) quick solution:-" +                            "suggestions welcome  :/  :( :)):(";             string output = replacemethod(input, dict1);              console.writeline("input");             console.writeline(input);             console.writeline();             console.writeline("output");                         console.writeline(output);              console.readline();         }     } } 

little disclaimer: haven't tested it.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -