Add markup code to specific words in a text

  • Thread starter Thread starter Toska
  • Start date Start date
T

Toska

Hi

Wonder how the should use the Regex members to add markup code to words
in a text.


Code:
--------------------

string words1 = "dog|cat|pig|horse|hippo";
string text1 = "hadsf das adh f dog adhsf yagds uyaudfyas gdf pig afsd. agfg afd gasd fgasd g dog. aygs fag fiusadf gi cat hasd horse, asdf pig. asdygf is dfisiau dfgisadf ias ufg dog.";

text1 = Regex.Replace(text1, somepattern, somereplace, someoptions);

Output:
hadsf das adh f [span class="blue"]dog[/span] adhsf yagds uyaudfyas gdf [span class="blue"]pig[/span] afsd. agfg afd gasd fgasd g [span class="blue"]dog[/span]. aygs fag fiusadf gi [span class="blue"]cat[/span] hasd [span class="blue"]horse[/span], asdf [span class="blue"]pig[/span]. asdygf is dfisiau dfgisadf ias ufg [span class="blue"]dog[/span].
 
Toska said:
Wonder how the should use the Regex members to add markup code to words
in a text.
To highlight words matched by the words1 expression found in text1 in
your example, you can use the following code (C#):
string words1 = "dog|cat|pig|horse|hippo";
string text1 = "hadsf das adh f dog adhsf yagds uyaudfyas gdf pig afsd.
agfg afd gasd fgasd g dog. aygs fag fiusadf gi cat ";

Regex r=new Regex(words1);
string result=r.Replace(text1,"<em>$&</em>");

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Back
Top