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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions 2

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello, been a while since I've been here. :)

Here's my question: I am writing a template class and I want to use Perl regular expressions. This is easy. However, I can't get the replace function to work.

For example, in Perl, all you have to do is:
[tt]
$_ =~ s/blah/replace/;
[/tt]

Which replaces blah with the word replace. How can I do this in ASP? This is the code I have which doesn't work:
[tt]
<%
set myreg = new RegExp
myreg.global = true
myreg.ignorecase = true
myreg.pattern = &quot;/{{{[^>]+}}}/value/&quot;

set fs = CreateObject(&quot;scripting.filesystemobject&quot;)
set wfile = fs.opentextfile(&quot;E:\location\of\template\template.tpl&quot;)
filecontent = wfile.readall

wfile.close
set wfile=nothing
set fs=nothing
set g_matches = myreg.execute(filecontent)

response.write(filecontent)
%>
[/tt]

Does ASP not support the ability to do this? If not, how can I get around this flaw? Basically, I want to replace everything in three braces ({{{blah}}}) with a value set in an array. Are regular expressions the best way to do this?

Thanks for the help,
-Vic vic cherubini
krs-one@cnunited.com
 
hmm...
try this one
<html>
<body>
<%
dim str, myreg, newstr
str = &quot;this is a paragraph that {{{blah}}} contains {{{blah}}} absolutely nothing {{{blah}}}&quot;
response.write &quot;before replace &quot; & str & &quot;<br>&quot;

set myreg = new RegExp
myreg.pattern = &quot;(\{{3}[a-zA-Z0-9_]+\}{3})&quot;
myreg.global = true
myreg.ignorecase = true
newstr = myreg.replace(str,&quot;&quot;)

response.write &quot;after replace &quot; & newstr
%>
</body>
</html>

I'm going to straighten out the regex pattern but for a quick write it works _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
onpnt2.gif
[sup] [/sup]
 
Thanks for the response. One question though: how do I replace the values found in the string with values in an array. For example, I modified the code to read:

<%
dim str, myreg, newstr

str = &quot;this is a paragraph that {{{blah}}} contains {{{blah}}} absolutely nothing {{{blah}}}&quot;
response.write &quot;before replace &quot; & str & &quot;<br />&quot;

dim replaced(3)
replaced(0) = &quot;value 1&quot;
replaced(1) = &quot;value 2&quot;
replaced(2) = &quot;value 3&quot;

set myreg = new RegExp
myreg.pattern = &quot;(\{{3}[a-zA-Z0-9_]+\}{3})&quot;
myreg.global = true
myreg.ignorecase = true

for i = 0 to 2
newstr = myreg.replace(str, replaced(i))
next

response.write &quot;after replace &quot; & newstr

%>

However, all thats replaced is with &quot;value 3&quot; since its the last value in the array. Is there a way to replace all instances of whatever you find in the search with an array like in PHP?

Thanks,
-Vic vic cherubini
vic @ openglforums.com
 
try looping to ubound of the array instead
eg
for i = 0 to UBound(replaced)
newstr = myreg.replace(str, replaced(i))
next _________________________________________________________
for the best results to your questions: FAQ333-2924
[sub]01001111 01101110 01110000 01101110 01110100[/sub]
onpnt2.gif
[sup] [/sub]
 
Another way to look at the same problem, you could use the join function and an odd character/string to join the array into a single string, do your replace, then use the split function to break it back up into an array :)
Code:
Dim replacedStr
replacedStr = Join(replaced,&quot;~PARKINGTICKET~&quot;)
replacedStr = myreg.replace(str,replacedStr)
replace = Split(replacedStr,&quot;~PARKINGTICKET~&quot;)

That example string masy be longer than necessary, but I needed something that would stand out as an example :)

-Tarwn

[/code] [sup]01001101 01101101 01101101 00101110 00101110 00101110 01000011 01101111 01100110 01100110 01100101 01100101[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Thanks for the replies, but I'm still having some trouble. The string that reads &quot;this is a paragraph that {{{var1}}} contains {{{var2}}} absolutely nothing {{{var3}}}&quot; should read &quot;this is a paragrap that value 1 contains value 2 nothing value 3&quot;

I want to replace the {{{var#}}} with a coresponding value in the replaced array.

What am I missing. Is this possible (I would certainly hope so!)

-Vic vic cherubini
vic @ openglforums.com
 
hi vcherubini
can you post the funtion that you have doing the work so we can get a better idea of what the problem may be _________________________________________________________
for the best results to your questions: FAQ333-2924
[sub]01001111 01101110 01110000 01101110 01110100[/sub]
onpnt2.gif
[sup] [/sub]
 
The loop will work like onpnt had above, with one minor change, global should be false :)
ie, rather than replace globally, replace first match.

-Tarwn [sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Thanks for the help guys, but turning global off doesn't work.

Here is my code in its entirity:
[tt]
<%
dim str, myreg, newstr

str = &quot;this is a paragraph that {{{var1}}} contains {{{var2}}} absolutely nothing {{{var3}}}&quot;

dim replaced(3)
replaced(0) = &quot;value 1&quot;
replaced(1) = &quot;value 2&quot;
replaced(2) = &quot;value 3&quot;

set myreg = new RegExp
myreg.pattern = &quot;(\{{3}[a-zA-Z0-9_]+\}{3})&quot;
myreg.global = false
myreg.ignorecase = true

set my_matches = myreg.execute(str)

for i = 0 to ubound(replaced) - 1
newstr = myreg.replace(str, replaced(i))
next

response.write newstr

%>
[/tt]

Now, what I want to happen is for {{{var1}}} to be replaced with &quot;value 1&quot;, {{{var2}}} to be replaced with &quot;value 2&quot;, so on and so forth.

How should I go about doing this?

Thanks for all the help so far though.

-Vic vic cherubini
vic @ openglforums.com
 
Oh, sorry, didn't notice this before. You keep replacing the first value you find in str and assiging it to newstr. This means that no matter how many matches there are it will only match that first occurrence everytime, becuse every time your matching on the str variable as it was before you started. Try changing your loop to this:
Code:
newstr = str
for i = 0 to ubound(replaced) - 1 
    newstr = myreg.replace(newstr, replaced(i))
next

Now if you actually wante to look for something like {{{var #}}} and then output value #, ie whatever number was in the original string, let me know. I wasn't sure which method you were aiming at but since we wer so far along with this I figured I would answer it before expanding the question :)

-Tarwn [sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
That works perfectly! Thanks a ton for the help.

-Vic vic cherubini
vic @ openglforums.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top