regEx.Repalce help

I

Igal

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.
 
A

Anthony Jones

Igal said:
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>")
 
I

Igal

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.
 
A

Anthony Jones

Igal said:
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.

Creates a pattern which finds any of the search words. It places the found
search word in sub-match $1.

The pattern will be used iteratively untill no further matches are found

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

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).
 
I

Igal

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.
 
A

Anthony Jones

Igal said:
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.
 
A

Anthony Jones

Anthony Jones said:
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.
 
I

Igal

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.
 
A

Anthony Jones

Igal said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,802
Members
47,349
Latest member
eixataze

Latest Threads

Top