473,396 Members | 1,683 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,396 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 1349
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.