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

'Replace' and RegExp question 2

Status
Not open for further replies.

Silx

Programmer
Sep 19, 2007
31
US
Greetings all.

I'm looking for some assistance in a small vbscript problem that has my mind bending into pretzels. :x

Let's say we have a string like this:

Code:
"abcdefghijklmnop <br> abcdefghijklmnop <br> abcdefghijk [tag] abcdefghijklmnopqr <br> abcdefghijklmnop <br> [/tag] abcdefghijklmnop <br> abcdefghijklmnop <br>"

Basically what I want to do with that string is replace all the <br>'s with <p>'s in that string EXCEPT for the <br>'s in between the [tag] and [/tag]. Is this possible programmaticaly? I was debating regular expressions, but wouldn't that only flag what's inside [tag] and [/tag] and not what's outside?

Any help is appreciated!

-S
 
[0] One of the main complication comes from the possibility of nested [tag][/tag]. (If no nesting ever happens, the problem simplifies itself a lot.)

[1] This is how you can do it with style. The main construction consists of a function. In the function, there is a main nesting validation process. These are the key points to read the construction.
[tt]
function replace_ex(s_in, byref s_out)

'array c(i) is vital for nesting validation
'c(i) is the _accumulated_ [/tag] encountered up to the array index i of a (splitting [tag])
'c{i} must be less or equal to index i to be well-nested (c{i)<=i)
'For c(i)=i, the last component of splitting a(i) wrt "[/tag]" can apply the replace operation, if any.
'For c(i)<i, no replacement operation should perform.
'For c(i)>i, nesting is erronous, no replacement is ever performed on the input string.

dim t,a,ab,c,bvalid,rx
t=s_in
bvalid=true
a=split(t,"[tag]")
redim c(ubound(a)) 'accumulate count of close [/tag] tag
'valdation of nesting, if failed, nothing will be replaced
for i=0 to ubound(a)
ab=split(a(i),"[/tag]")
if i>0 then
c(i)=c(i-1)+ubound(ab)
else
c(i)=ubound(ab)
end if
if c(i)>i then
bvalid=false
exit for
end if
next

if bvalid then
'use of regexp is just for some flexibility
'for simple rigid setting, split/replace would do
set rx=new regexp
with rx
.pattern="<br\s*\/>"
.global=true
end with
for i=0 to ubound(a)
if c(i)=i then
ab=split(a(i),"[/tag]")
ab(ubound(ab))=rx.replace(ab(ubound(ab)),"<p>")
a(i)=join(ab,"[/tag]")
end if
next
end if
t=join(a,"[tag]")
s_out=t
replace_ex=bvalid
end function
[/tt]
[2] The use of it is like this.
[tt]
'I slightly change it to include nested [tag][/tag]
dim s,t,bvalid
s="abcdefghijklmnop <br> abcdefghijklmnop <br> abcdefghijk [tag] abc<br>defg[tag]hijklm[/tag]nopqr <br> abcdefghijklmnop <br> [/tag] abcdefghijklmnop <br> abcdefghijklmnop <br>"

t=""
bvalid=replace_ex(s,t)
if bvalid then
response.write escape(t) & "<br />"
else
response.write "nesting of [tag][/tag] is invalid<br />" & escape(s) & "<br />"
end if
[/tt]
 
Works like a CHARM! Wish I could give you more than 1 thanks. :D

I really appreciate the help. tek-tips.com always seems to come through with the help I need.

cheers!
 
I think I spoke too soon.
After testing, it looks like it's replacing the <br>'s throughout the text. Even if they're outside the
Code:
tags. hmm.
 
erratum
Sorry, I typed up missing a question mark (?) here.
>[self].pattern="<br\s*\/>"
[tt].pattern="<br\s*\/[COLOR=red yellow]?[/color]>"[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top