473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Regex again


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:
--------------------

1 2
"swswswdogswswsw swsw dog"

--------------------
Both "dog" is captured but how can I make so only dog number 2 gets
captured?

--
Toska
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 19 '05 #1
2 1334
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Toska" <To**********@mail.codecomments.com> wrote in message
news:To**********@mail.codecomments.com...

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:
--------------------

1 2
"swswswdogswswsw swsw dog"

--------------------
Both "dog" is captured but how can I make so only dog number 2 gets
captured?

--
Toska
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 19 '05 #2
On Mon, 31 Jan 2005 20:06:40 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
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.

--
Iain Norman | http://www.eliteforum.org
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
8
by: vbmark | last post by:
I'm new to RegEx in vb.net so I'm not sure how to do this. I want to know if a string contains two minus signs "-". If there are two then I want it to return TRUE. I also need to know if the...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
7
by: CSharper | last post by:
I have a situation where I have file with all the find and replace values, I read it and put it in a dictionary format. Next I have to read all the files in a given directory and read each line in...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.