Replace() whole words 
Author Message
 Replace() whole words

Hi,

Let's say I have the following string: sSTR = "the blue blues guitar".
If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
greens guitar".

Is there a way to replace whole words instead of just substrings like the
Replace function does?

Thanks,

Gilles



Mon, 26 Jul 2004 02:56:38 GMT  
 Replace() whole words
Use a Regular Expression...

sSTR = "the blue blues guitar is blue"
set re = new regexp
re.pattern = "\bblue\b"
re.global = true
re.ignorecase = true
sNew = re.replace(sStr,"green")

msgbox sNew

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> Hi,

> Let's say I have the following string: sSTR = "the blue blues guitar".
> If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
> greens guitar".

> Is there a way to replace whole words instead of just substrings like the
> Replace function does?

> Thanks,

> Gilles



Mon, 26 Jul 2004 10:14:43 GMT  
 Replace() whole words
I  tried using RegExp but here's the problem:
What I am writting is some sort of search engine. The user enters all the
keywords to search in a whole page and the result of this search is the same
page but with the search key words in red. I gave regExp a try but i either
didn't do it correctly or it isn't the right tool for the job.

strSearch  is the string that contains all the keywords seperated by comas.
strText is the text or the page read from the DB.

arKW = Split(strSearch,",") ' Sets the keywords into an array

Set re = New RegExp
re.global=true
re.ignorecase=true
For i = 0 To UBound(arKW)-1
    re.pattern = arKW(i)
    strResult = strResult & re.replace(strText,"<font color='Red'>" &
arKW(i) & "</font>")
Next

Or something close to this (I don't have the code right here).
What happened was a long loop that killed the machine - 256 RAM, 800MHz AMD,
Win2000Pro, IIS 5. The memory usage got to almost 100% and then stopped
responding.

Any ideas?

Gilles



Use a Regular Expression...

sSTR = "the blue blues guitar is blue"
set re = new regexp
re.pattern = "\bblue\b"
re.global = true
re.ignorecase = true
sNew = re.replace(sStr,"green")

msgbox sNew

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:
> Hi,

> Let's say I have the following string: sSTR = "the blue blues guitar".
> If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
> greens guitar".

> Is there a way to replace whole words instead of just substrings like the
> Replace function does?

> Thanks,

> Gilles



Tue, 27 Jul 2004 00:03:05 GMT  
 Replace() whole words
Can you not simply do:

sSTR = Replace(sSTR," blue "," green ")

??

tom.


Quote:
> I  tried using RegExp but here's the problem:
> What I am writting is some sort of search engine. The user enters all the
> keywords to search in a whole page and the result of this search is the
same
> page but with the search key words in red. I gave regExp a try but i
either
> didn't do it correctly or it isn't the right tool for the job.

> strSearch  is the string that contains all the keywords seperated by
comas.
> strText is the text or the page read from the DB.

> arKW = Split(strSearch,",") ' Sets the keywords into an array

> Set re = New RegExp
> re.global=true
> re.ignorecase=true
> For i = 0 To UBound(arKW)-1
>     re.pattern = arKW(i)
>     strResult = strResult & re.replace(strText,"<font color='Red'>" &
> arKW(i) & "</font>")
> Next

> Or something close to this (I don't have the code right here).
> What happened was a long loop that killed the machine - 256 RAM, 800MHz
AMD,
> Win2000Pro, IIS 5. The memory usage got to almost 100% and then stopped
> responding.

> Any ideas?

> Gilles



> Use a Regular Expression...

> sSTR = "the blue blues guitar is blue"
> set re = new regexp
> re.pattern = "\bblue\b"
> re.global = true
> re.ignorecase = true
> sNew = re.replace(sStr,"green")

> msgbox sNew

> --
> Michael Harris
> Microsoft.MVP.Scripting
> --


> > Hi,

> > Let's say I have the following string: sSTR = "the blue blues guitar".
> > If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
> > greens guitar".

> > Is there a way to replace whole words instead of just substrings like
the
> > Replace function does?

> > Thanks,

> > Gilles



Tue, 27 Jul 2004 00:48:42 GMT  
 Replace() whole words
This works OK for me...

Set re = New RegExp
re.global=true
re.ignorecase=true

arKW = Array("red","white","blue")

strText = "The American flag has red and white stripes and white stars on a field of blue."
strResult = strText

For i = 0 To UBound(arKW)
    re.pattern = "\b" & arKW(i) & "\b"
    strResult = re.replace(strResult,"<font color='Red'>" & arKW(i) & "</font>")
Next

msgbox strResult

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> I  tried using RegExp but here's the problem:
> What I am writting is some sort of search engine. The user enters all the
> keywords to search in a whole page and the result of this search is the same
> page but with the search key words in red. I gave regExp a try but i either
> didn't do it correctly or it isn't the right tool for the job.

> strSearch  is the string that contains all the keywords seperated by comas.
> strText is the text or the page read from the DB.

> arKW = Split(strSearch,",") ' Sets the keywords into an array

> Set re = New RegExp
> re.global=true
> re.ignorecase=true
> For i = 0 To UBound(arKW)-1
>     re.pattern = arKW(i)
>     strResult = strResult & re.replace(strText,"<font color='Red'>" &
> arKW(i) & "</font>")
> Next

> Or something close to this (I don't have the code right here).
> What happened was a long loop that killed the machine - 256 RAM, 800MHz AMD,
> Win2000Pro, IIS 5. The memory usage got to almost 100% and then stopped
> responding.

> Any ideas?

> Gilles



> Use a Regular Expression...

> sSTR = "the blue blues guitar is blue"
> set re = new regexp
> re.pattern = "\bblue\b"
> re.global = true
> re.ignorecase = true
> sNew = re.replace(sStr,"green")

> msgbox sNew

> --
> Michael Harris
> Microsoft.MVP.Scripting
> --


> > Hi,

> > Let's say I have the following string: sSTR = "the blue blues guitar".
> > If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
> > greens guitar".

> > Is there a way to replace whole words instead of just substrings like the
> > Replace function does?

> > Thanks,

> > Gilles



Tue, 27 Jul 2004 01:35:22 GMT  
 Replace() whole words
It's ok. I'm just stupid. I forgot the "\b" in the pattern. Once I added
them all is fine.

Thanks for your help Michael.

Gilles


This works OK for me...

Set re = New RegExp
re.global=true
re.ignorecase=true

arKW = Array("red","white","blue")

strText = "The American flag has red and white stripes and white stars on a
field of blue."
strResult = strText

For i = 0 To UBound(arKW)
    re.pattern = "\b" & arKW(i) & "\b"
    strResult = re.replace(strResult,"<font color='Red'>" & arKW(i) &
"</font>")
Next

msgbox strResult

--
Michael Harris
Microsoft.MVP.Scripting
--


Quote:
> I  tried using RegExp but here's the problem:
> What I am writting is some sort of search engine. The user enters all the
> keywords to search in a whole page and the result of this search is the
same
> page but with the search key words in red. I gave regExp a try but i
either
> didn't do it correctly or it isn't the right tool for the job.

> strSearch  is the string that contains all the keywords seperated by
comas.
> strText is the text or the page read from the DB.

> arKW = Split(strSearch,",") ' Sets the keywords into an array

> Set re = New RegExp
> re.global=true
> re.ignorecase=true
> For i = 0 To UBound(arKW)-1
>     re.pattern = arKW(i)
>     strResult = strResult & re.replace(strText,"<font color='Red'>" &
> arKW(i) & "</font>")
> Next

> Or something close to this (I don't have the code right here).
> What happened was a long loop that killed the machine - 256 RAM, 800MHz
AMD,
> Win2000Pro, IIS 5. The memory usage got to almost 100% and then stopped
> responding.

> Any ideas?

> Gilles



> Use a Regular Expression...

> sSTR = "the blue blues guitar is blue"
> set re = new regexp
> re.pattern = "\bblue\b"
> re.global = true
> re.ignorecase = true
> sNew = re.replace(sStr,"green")

> msgbox sNew

> --
> Michael Harris
> Microsoft.MVP.Scripting
> --


> > Hi,

> > Let's say I have the following string: sSTR = "the blue blues guitar".
> > If I am using sSTR = Replace(sSTR,"blue","green") I will get "the green
> > greens guitar".

> > Is there a way to replace whole words instead of just substrings like
the
> > Replace function does?

> > Thanks,

> > Gilles



Tue, 27 Jul 2004 01:53:22 GMT  
 Replace() whole words
Tom,

The Replace function replaces *substrings*, not whole words. So if you have
the word "BLUEasdfasdf" it will be replaced by "GREENasdfasdf" because blue
is part of blueasdfasdf.

In other words it doesn't work for replacing just full words.

But Michael Harris gave me the solution - use Regular Expression (RegExp).


Quote:
> Can you not simply do:

> sSTR = Replace(sSTR," blue "," green ")

> ??

> tom.



Tue, 27 Jul 2004 01:58:41 GMT  
 Replace() whole words
... but not if you put spaces in your substrings, like I did ........

tom.


Quote:
> Tom,

> The Replace function replaces *substrings*, not whole words. So if you
have
> the word "BLUEasdfasdf" it will be replaced by "GREENasdfasdf" because
blue
> is part of blueasdfasdf.

> In other words it doesn't work for replacing just full words.

> But Michael Harris gave me the solution - use Regular Expression (RegExp).



> > Can you not simply do:

> > sSTR = Replace(sSTR," blue "," green ")

> > ??

> > tom.



Tue, 27 Jul 2004 17:32:05 GMT  
 Replace() whole words

Quote:
> ... but not if you put spaces in your substrings, like I did ........

You miss whole words next to punctuation.

  sSTR = "How blue is blue?"
  MsgBox Replace(sSTR," blue "," green ")
  With New RegExp
    .Pattern = "\bblue\b"
    .Global = True
    MsgBox .Replace(sSTR, "green")
  End With

--
Kindness in words creates confidence. Kindness in thinking creates
profoundness. Kindness in giving creates love. -Lao-Tzu

=-=-=
Steve
-=-=-



Tue, 27 Jul 2004 19:41:04 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Coded Mastersource does not work

2. Delphi Client/Server Cert Exam

3. Problems with filters

4. When Replace function will delete the whole sentence?

5. Why Replace() delete the whole string?

6. searching and replacing with wildcards the whole document

7. find/replace in a Word Doc using word object and vbscript

8. Merry Christmas to all Pascal-Freaks!!!

9. Replacing words/variables in a word document

10. Finding Word 6.0/95 files and replacing them in Word 2000 format

11. VB.Net/Word: Search and Replace on ALL instances of a word

12. How to copy the whole Word document page to the clipboard

 

 
Powered by phpBB® Forum Software