473,769 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

Avi
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi
Jun 27 '08 #1
8 2287
Avi wrote:
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?
In a regular expression pattern you can use \b to indicate a word
boundary so that this code

string example = "foo foobar barfoo foo";
string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
Console.WriteLi ne(result);

outputs

baz foobar barfoo baz
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write( myNewText)

"Avi" <re*********@ya hoo.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like
it to replace only full words that matches oldValue and not when oldValue
is a substring of a larger word.

How can it be done?

Thanks,
Avi
Jun 27 '08 #3
ThatsIT.net.au wrote:
Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write( myNewText)
That doesn't make it any better.

Given this replacement:

Dim oldWord = "all"
Dim newWord = "none"

you would get the string "once upon a time in a smnone town "

Also notice the extra space added at the end...
"Avi" <re*********@ya hoo.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Hi all,

I'm using string Replace(string oldValue, string newValue) and would
like it to replace only full words that matches oldValue and not when
oldValue is a substring of a larger word.

How can it be done?

Thanks,
Avi

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4
Avi wrote:
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.

How can it be done?

Thanks,
Avi
As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(ol dValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(t ext, pattern, newValue);

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #5
On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.co mwrote:
Avi wrote:
Hi all,
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.
How can it be done?
Thanks,
Avi

As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(ol dValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(t ext, pattern, newValue);

--
Göran Andersson
_____http://www.guffa.com


Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
Console.WriteLi ne(result);
Works out nicely.
-Cnu
Jun 27 '08 #6
Duggi wrote:
On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.co mwrote:
>Avi wrote:
>>Hi all,
I'm using string Replace(string oldValue, string newValue) and would like it
to replace only full words that matches oldValue and not when oldValue is a
substring of a larger word.
How can it be done?
Thanks,
Avi
As Martin suggested, the /b code in a regular expressions is the solution.

Put the word to look for in a regular expression by using the Escape method:

string pattern = "/b" + Regex.Escape(ol dValue) + "/b";

Now you can use that to match the words:

text = Regex.Replace(t ext, pattern, newValue);

--
Göran Andersson
_____http://www.guffa.com

Thanks for the help.

string example = "foo foobar barfoo foo";
string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
Console.WriteLi ne(result);
Works out nicely.
-Cnu
Yes, that works if you have full control over the input, so that you can
be sure that it doesn't contain any characters that have a special
meaning in a regular expression.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #7
Avi
Thank you all for your help!

Avi
"Avi" <re*********@ya hoo.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Hi all,

I'm using string Replace(string oldValue, string newValue) and would like
it to replace only full words that matches oldValue and not when oldValue
is a substring of a larger word.

How can it be done?

Thanks,
Avi

Jun 27 '08 #8
Yes your right, didn't think it though, A regex is the way to go.
"Göran Andersson" <gu***@guffa.co mwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
ThatsIT.net.au wrote:
> Dim myText As String = "once upon a time in a small town"
Dim myNewText As String = String.Empty
Dim oldWord = "small"
Dim newWord = "large"
Dim words() As String = Split(myText, " ")

For Each word In words
myNewText += Replace(word, oldWord, newWord) & " "
Next

Response.Write( myNewText)

That doesn't make it any better.

Given this replacement:

Dim oldWord = "all"
Dim newWord = "none"

you would get the string "once upon a time in a smnone town "

Also notice the extra space added at the end...
>"Avi" <re*********@ya hoo.comwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
>>Hi all,

I'm using string Replace(string oldValue, string newValue) and would
like it to replace only full words that matches oldValue and not when
oldValue is a substring of a larger word.

How can it be done?

Thanks,
Avi


--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #9

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

Similar topics

4
10200
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function filter() { var items = new Array("John", "Jane");
19
78833
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
2
1433
by: Raed Sawalha | last post by:
I have the following text:- Brian went to stadium to watch the soccer game, Brian MacWoods is bussiness man and very rich man. Brian likes to run every morning on beachside. the problem i have I get the list of words that should be replace in the provided text as follows:- Brian (ONLY) : should be replaced by Mr with Brian word itself==> will be
7
43623
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring). Is there something equivalent in C#, preferably built-in (assumed better performance), or sample code? Thanks in advance.
8
4995
by: shapper | last post by:
Hello, I have a string which holds a text. Is it possible to create a substring which uses the first N words of that string? Thanks, Miguel
0
2079
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how I'd like. Here's the code I'm now using: static string CIR(string input, string oldValue, string newValue) { Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Singleline);
10
2194
by: Robert R. | last post by:
Hello, i would like to write a piece of code to help me to align some sequence of words and suggest me the ordered common subwords of them s0 = "this is an example of a thing i would like to have".split() s1 = "another example of something else i would like to have".split() s2 = 'and this is another " example " but of something ; now i would still like to have'.split() ....
7
3138
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using std::ifstream; using std::ofstream; #include <stdio.h> #include <cstdlib> #include <string>
3
3963
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code below and it works fine as long as I'm not adding tags around the to parameter. Can anyone explain to me why it doesn't work with tags? And it needs to be XSLT 1.0. This works: X<xsl:value-of select="'little steak'"/>X This doesn't work:...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.