Add markup code to specific words in a text

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].
 
?

=?ISO-8859-1?Q?Anders_Nor=E5s?=

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top