Regex again

T

Toska

Hi again

Follow up on my previous thread where I got help to enclose specific
words in markup code. After some modifications it looks like this:

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

void SomeMethod()
{
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 ";

text1 = Regex.Replace(text, "dog|cat|pig|horse|hippo", new MatchEvaluator(MarkupBuilder));
}

static string MarkupBuilder(Match m)
{
string x = m.ToString();

if(...)
return ...;

return ...;
}

--------------------
The above works well, but the new problem is that when a word is a part
of another word it is also captured. As follows.

Code:
 
K

Karl Seguin

Toska:
\b is your friend...try

\bdog\b|\bcat\b|\bpig\b

also, note that you probably want to turn the IgnoreCase option on.

new MatchEvaluator(...), RegexOptiions.IgnoreCase);

Hope this helps,
Karl
 
T

teknohippy

Toska:
\b is your friend...try

\bdog\b|\bcat\b|\bpig\b

also, note that you probably want to turn the IgnoreCase option on.

new MatchEvaluator(...), RegexOptiions.IgnoreCase);

Hope this helps,
Karl

For the benefit of the OP, \b matches at a position known as the "word
boundry"

4 Position qualify:

Before the first character in the string, if the first character is a
word character.

After the last character in the string, if the last character is a
word character.

Between a word character and a non-word character following right
after the word character

Between a non-word character and a word character following right
after the non-word character.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top