Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass $1 from regex to a function

Status
Not open for further replies.

stakadush

Programmer
Oct 1, 2001
195
IL
hey
i need to pass the $1 variable from a regular expression to a function. here's what i'm trying to do:
Code:
strTemplateData = &quot;this is it<br>LinkToObject(44)&quot;

Set regEx = New RegExp
regEx.Pattern = &quot;LinkToObject *\( *&quot;&quot;? *(\d+?) *&quot;&quot;? *\)&quot;
regEx.IgnoreCase = False
regEx.Global = True
strTemplateData = regEx.Replace(strTemplateData, LinkToObject(&quot;$1&quot;))

Function LinkToObject(intObjectID)
	response.write &quot;here: &quot; & intObjectID&&quot;<br>&quot;
End Function
$1 is equal to 44,but when trying to pass it to the function what i get printed is here: $1 instead of here: 44.

looping through the Matches won't help becuase the match is LinkToObject(44) and not just 44.

thanks

:)
 
Thats because it is inside quotes. Now it looks like a string that is $1. Just pass like this:
LinkToObject($1)

Or if you want to, set a new variable like this:
newVar=$1
LinkToObject(newVar)

Either one should fix the problem.

Mike
 
i tried doing LinkToObject($1) and got an error message saying:
Code:
Invalid character

/test.asp, line 8

strTemplateData = regEx.Replace(strTemplateData, LinkToObject($1))
--------------------------------------------------------------^
and i can't set a new variable because i'm doing it inside the replace function,and besides i'll get the same error message.

:)
 
Ok,

First, sorry about the $1 versus &quot;$1&quot;. That's my perl interfering with my bad vbscript regexp use.

What I see is a bit confusing now that I take a better look.

What I would do is this:

if regex.test(strTemplateData) then
strTemplateData=regex.replace(strTemplateData, $1)
end if

What I see is you replacing the strTemplateData with a call to LinkToObject with $1 passed in. I don't see that working.

Also, the replace method just replaces the pattern inside the string.

Ex.
<script language=vbscript>
dim re, str, newstr
set re=new regexp

re.pattern=&quot;^\w+\s+(\w+)&quot;
str=&quot;i have a dog named joe.&quot;

if re.test(str) then
document.write &quot;Match&quot;
document.write &quot;<br>&quot;

newstr=re.replace(str, &quot;$1&quot;)


document.write newstr
end if
</script>

$1 matches &quot;have&quot;.

So I replace the pattern &quot;i have&quot; with &quot;have&quot;. The new string is &quot;have a dog named joe.&quot;

You appear to want to replace the original string with just $1. Ok, this is where my vbscript fails. I'm not sure how you should make that change.

My suggestion, try the Matches collection or Match object. I think that gives what was matched. I have never used it though. I would suggest checking it out at:

Sorry I couldn't help more.

Mike
 
thanks mike.
i thought that was perl interfering :)
if i'd want to do this in perl this is what i'd do:

$template_data =~ s/LinkToObject *\( *\&quot;? *(\d+?) *\&quot;? *\)/&link_to_object($1)/ge;

i want to pass $1 to the link_to_object sub and replace it with the result from &link_to_object.
using the matches collection is not good enough because the match is LinkToObject([number]) and not just [number].
any other ideas?

thanks

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top