467,146 Members | 1,293 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

regEx.Repalce help

hay. i have read some topics about this. but there's something i don't
quite understand, if anyone can help.

i have text, in it i want to highlight the words user used to find this
result.
i used the normal replace, it worked fine except the case thing...

SearchForSp = Split(SearchFor, " ")
For i = 0 to Ubound(SearchForSp)
EmpJobsText = Replace(EmpJobsText, SearchForSp(i), "<strong><u><font
color=red>"&SearchForSp(i)&"</font></u></strong>", vbTextCompare)
EmpJobsText
next

i tried to use the regEx.Repalce from the examples in groups but just
couldn't find a way. problem is in the normal replace function i have
option to supply a source. in this case "EmpJobsText" is the source.
but regEx.Repalce don't seem to have place to type where to replace,
only what to replace with what.

Nov 29 '06 #1
  • viewed: 1498
Share:
9 Replies

"Igal" <ig********@gmail.comwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
hay. i have read some topics about this. but there's something i don't
quite understand, if anyone can help.

i have text, in it i want to highlight the words user used to find this
result.
i used the normal replace, it worked fine except the case thing...

SearchForSp = Split(SearchFor, " ")
For i = 0 to Ubound(SearchForSp)
EmpJobsText = Replace(EmpJobsText, SearchForSp(i), "<strong><u><font
color=red>"&SearchForSp(i)&"</font></u></strong>", vbTextCompare)
EmpJobsText
next

i tried to use the regEx.Repalce from the examples in groups but just
couldn't find a way. problem is in the normal replace function i have
option to supply a source. in this case "EmpJobsText" is the source.
but regEx.Repalce don't seem to have place to type where to replace,
only what to replace with what.
Assumptions:
Words in SearchFor contain only ASCII alphanumeric characters
Each word is seperated by a single space
Your search finds only whole words.

Dim rgx : Set rgx = New RegExp
rgx.Pattern = "\b(" & Replace(SearchFor, " ", "|") & ")\b"
rgx.Global = True
rgx.IgnoreCase = True

MsgBox rgx.Replace(EmpJobsText, "<u>$1</u>")

Nov 29 '06 #2
i can't see how this can help me. i'll try to to be more clear.

SearchFor = the search words user typed
EmpJobsText = SQL result with a lot of text

SearchForSp = Split(SearchFor, " ") = this line splits the search words
by the spaces between them

// this begins a loop that runs the number of words user typed.
For i = 0 to Ubound(SearchForSp)
// inside this loop i need to replace words Inside EmpJobsText that =
SearchForSp(i) with SearchForSp(i) but with html tags around it
(<b>SearchForSp(i)</b>)
next

i don't want the replace will be case sensitive.
Assumptions:
Words in SearchFor contain only ASCII alphanumeric characters
Each word is seperated by a single space
Your search finds only whole words.

Dim rgx : Set rgx = New RegExp
rgx.Pattern = "\b(" & Replace(SearchFor, " ", "|") & ")\b"
rgx.Global = True
rgx.IgnoreCase = True

MsgBox rgx.Replace(EmpJobsText, "<u>$1</u>")
Nov 30 '06 #3

"Igal" <ig********@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
i can't see how this can help me. i'll try to to be more clear.
Interesting. As I understand it you want to be able to highlight the search
words in the body of text that was searched without affecting the case used
by the original text.
>
SearchFor = the search words user typed
EmpJobsText = SQL result with a lot of text

SearchForSp = Split(SearchFor, " ") = this line splits the search words
by the spaces between them

// this begins a loop that runs the number of words user typed.
For i = 0 to Ubound(SearchForSp)
// inside this loop i need to replace words Inside EmpJobsText that =
SearchForSp(i) with SearchForSp(i) but with html tags around it
(<b>SearchForSp(i)</b>)
next

i don't want the replace will be case sensitive.
Assumptions:
Words in SearchFor contain only ASCII alphanumeric characters
Each word is seperated by a single space
Your search finds only whole words.

Dim rgx : Set rgx = New RegExp
rgx.Pattern = "\b(" & Replace(SearchFor, " ", "|") & ")\b"
Creates a pattern which finds any of the search words. It places the found
search word in sub-match $1.
rgx.Global = True
The pattern will be used iteratively untill no further matches are found
rgx.IgnoreCase = True
The case of the search words is not material. Note that it is the text with
it's current case as found in the search text which is placed in sub-match
$1

MsgBox rgx.Replace(EmpJobsText, "<u>$1</u>")
Ok is this where it confused. The MsgBox is simply there since I tested in
VBS. Clearly what you need is:-

EmpJobsText = rgx.Replace(EmpJobsText, "<u>$1</u>")

It replaces each search word with the same word (with it's case preserved)
with the underline mark up. (I was sure you could see how to add you
additional mark up requirements to it).
>

Nov 30 '06 #4
Okay, got it now. thanks a lot for the help!

Nov 30 '06 #5
oh, one more thing. you said this only works on ASCII characters.
how i make this work on all characters?
cause sometimes the search won't be in English.

Nov 30 '06 #6

"Igal" <ig********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
oh, one more thing. you said this only works on ASCII characters.
how i make this work on all characters?
cause sometimes the search won't be in English.
Hmm that probably won't be a problem. The version of Regular Expressions
implemented by VBScript doesn't only officially supports ASCII characters.
Hence /w (any word character) matches [A-Za-z0-9_] which clearly isn't a
wide enough range. However RegExp it will match words using characters
outside the ASCII range. For example the word féru can be searched for and
will match.

I think that if you want to start matching across the wider Unicode range
then you will have problems but as long as each search is limited to the a
single codepage (such as ISO-8859-1) then you will be ok.


Nov 30 '06 #7

"Anthony Jones" <An*@yadayadayada.comwrote in message
news:u1**************@TK2MSFTNGP02.phx.gbl...
>
"Igal" <ig********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
oh, one more thing. you said this only works on ASCII characters.
how i make this work on all characters?
cause sometimes the search won't be in English.

Hmm that probably won't be a problem. The version of Regular Expressions
implemented by VBScript doesn't only officially supports ASCII characters.
Once I read that back it makes no sense :(

It's meant to say 'The version of Regular Expressions implemented by
VBScript only officially supports ASCII characters.'

Hence /w (any word character) matches [A-Za-z0-9_] which clearly isn't a
wide enough range. However RegExp it will match words using characters
outside the ASCII range. For example the word féru can be searched for
and
will match.

I think that if you want to start matching across the wider Unicode range
then you will have problems but as long as each search is limited to the a
single codepage (such as ISO-8859-1) then you will be ok.


Nov 30 '06 #8
well, i didn't want to use this option but did.

SearchFor = lcase(SearchFor)
SearchForSp = Split(SearchFor, " ")
For i = 0 to Ubound(SearchForSp)
EmpJobsText = Replace(lcase(EmpJobsText), SearchForSp(i),
"<strong><font color=red>"&SearchForSp(i)&"</font></strong>",
vbTextCompare)
next

doesn't really matter cause most of the text is in hebrew
(Windows-1255) that don't have upper/lower case.

Nov 30 '06 #9

"Igal" <ig********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
well, i didn't want to use this option but did.

SearchFor = lcase(SearchFor)
SearchForSp = Split(SearchFor, " ")
For i = 0 to Ubound(SearchForSp)
EmpJobsText = Replace(lcase(EmpJobsText), SearchForSp(i),
"<strong><font color=red>"&SearchForSp(i)&"</font></strong>",
vbTextCompare)
next

doesn't really matter cause most of the text is in hebrew
(Windows-1255) that don't have upper/lower case.
Well if it works for you. Bear in mind though if some text isn't in hebrew
then you will lose case. Also the Regular expression approach performs the
replace in a single pass. If your search text is large then, depending on
the number of words searched for, the code could well run quite slow. Have
you also considered that the code above will highlight all occurances of the
word even if it is embedded in another word. E.g in that last sentence the
search for 'red bed' would highlight 'bed' in 'embedded' and 'red' in
'considered'.

Question, are you sure the regular expression doesn't work for you? Since
the full hebrew set is not available in Windows-1255 then it is possible
that RegExp doesn't fully meet your needs. However if the original source
of the search text is stored using Windows-1255 encoding then RegExp should
do what you need.

Nov 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by clintonG | last post: by
7 posts views Thread by Mike Labosh | last post: by
9 posts views Thread by jmchadha@gmail.com | last post: by
7 posts views Thread by Extremest | last post: by
1 post views Thread by DavidB | last post: by
6 posts views Thread by Phil Barber | last post: by
reply views Thread by Support Desk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.