Regular Expression Replace : $1 shows up as literal.  
Author Message
sabdillah





PostPosted: Mon Jul 11 22:13:10 CDT 2005 Top

VB Scripts >> Regular Expression Replace : $1 shows up as literal. Hi There.

I have searched the news groups and cannot seem to find an answer to my
situation. Basically, I have the following function (that appears
standard across all posts):

Public Function Highlight(p_sFind, p_sSearch)

Dim RegEx
Set RegEx = New RegExp
RegEx.Pattern=p_sFind
RegEx.IgnoreCase=True
RegEx.Global=True
Highlight = RegEx.Replace(p_sSearch,"<b>$1</b>")

End Function


However, wheneven I execute it on some code, I get a literal "$1"
rather then the string I searched for.

For example, Highlight ("Harv", "Harvard Medical School") returns:

"<b>$1</b>ard Medical School" rather than "<b>Harv</b>ard Medical
School".

Does anyone have any idea what I am doing wrong. Is any server
requirements in using this tool?

Thanks
Jason

Visual Studio215  
 
 
James





PostPosted: Mon Jul 11 22:13:10 CDT 2005 Top

VB Scripts >> Regular Expression Replace : $1 shows up as literal. "jason7069" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> Hi There.
>
> I have searched the news groups and cannot seem to find an answer to my
> situation. Basically, I have the following function (that appears
> standard across all posts):
>
> Public Function Highlight(p_sFind, p_sSearch)
>
> Dim RegEx
> Set RegEx = New RegExp
> RegEx.Pattern=p_sFind
> RegEx.IgnoreCase=True
> RegEx.Global=True
> Highlight = RegEx.Replace(p_sSearch,"<b>$1</b>")
>
> End Function
>
>
> However, wheneven I execute it on some code, I get a literal "$1"
> rather then the string I searched for.
>
> For example, Highlight ("Harv", "Harvard Medical School") returns:
>
> "<b>$1</b>ard Medical School" rather than "<b>Harv</b>ard Medical
> School".
>
> Does anyone have any idea what I am doing wrong. Is any server
> requirements in using this tool?

One small change should correct your problem. You need to put your search
string inside of parenthesis. Change the RegEx.Pattern line to this:

RegEx.Pattern="(" & p_sFind & ")"


 
 
jason7069





PostPosted: Mon Jul 11 23:10:55 CDT 2005 Top

VB Scripts >> Regular Expression Replace : $1 shows up as literal. That did it. Thanks a ton!

- Jason

 
 
Steve





PostPosted: Tue Jul 12 06:33:29 CDT 2005 Top

VB Scripts >> Regular Expression Replace : $1 shows up as literal. James Whitlow wrote:

> "jason7069" <EMail@HideDomain.com> wrote in message
> news:EMail@HideDomain.com...
>>
>> I have searched the news groups and cannot seem to find an answer to my
>> situation. Basically, I have the following function (that appears
>> standard across all posts):
>>
>> Public Function Highlight(p_sFind, p_sSearch)
>>
>> Dim RegEx
>> Set RegEx = New RegExp
>> RegEx.Pattern=p_sFind
>> RegEx.IgnoreCase=True
>> RegEx.Global=True
>> Highlight = RegEx.Replace(p_sSearch,"<b>$1</b>")
>>
>> End Function
>>
>>
>> However, wheneven I execute it on some code, I get a literal "$1"
>> rather then the string I searched for.
>>
>> For example, Highlight ("Harv", "Harvard Medical School") returns:
>>
>> "<b>$1</b>ard Medical School" rather than "<b>Harv</b>ard Medical
>> School".
>>
>> Does anyone have any idea what I am doing wrong. Is any server
>> requirements in using this tool?
>
> One small change should correct your problem. You need to put your search
> string inside of parenthesis. Change the RegEx.Pattern line to this:
>
> RegEx.Pattern="(" & p_sFind & ")"

Or change the replace string to use the entire matched string (&$)
instead of the first submatch ($1):

Highlight = RegEx.Replace(p_sSearch,"<b>$&</b>")

Capturing submatches slows down regular expressions, so any time you
can avoid them, that's a Good Thing.

Once again, the VBScript docs are imcomplete when it comes to regular
espressions! You have to go to the JScript docs for the good stuff. <g>

JScript replace Method
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreplace.asp

Download Windows Script Documentation
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9

--
Steve

A great many people mistake opinions for thoughts. -Herbert V. Prochnow
 
 
James





PostPosted: Tue Jul 12 07:17:39 CDT 2005 Top

VB Scripts >> Regular Expression Replace : $1 shows up as literal. "Steve Fulton" <EMail@HideDomain.com> wrote in message
news:op.stss1x1ii0ix9l@steve...
> Or change the replace string to use the entire matched string (&$)
> instead of the first submatch ($1):
>
> Highlight = RegEx.Replace(p_sSearch,"<b>$&</b>")
>
> Capturing submatches slows down regular expressions, so any time you
> can avoid them, that's a Good Thing.
>
> Once again, the VBScript docs are imcomplete when it comes to regular
> espressions! You have to go to the JScript docs for the good stuff. <g>
>
> JScript replace Method
> http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthreplace.asp
>
> Download Windows Script Documentation
>
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9

Thanks, Steve!

You are correct about the documentation; I was not aware of $&. The link
was also quite informative. I was also not aware of $` and $'.