?
Solved

need regexp to replace nested tag

Posted on 2005-03-15
17
Medium Priority
?
323 Views
Last Modified: 2012-06-21
hi! I've the follow codes to replace [quote][/quote] tag. but it is not working for nested tags, for example:

'this works
[quote]this is a test quote[/quote]

'this doesn't work
[quote]this is a test quote with [quote] nested test code[/quote][/quote]

this 1st open quote takes the 1st close quote which is wrong. the 1st open quote should take the 2nd close quote instead. Any idea how to make this work?

Function FormatQuote(content)
      set r = new regexp
      r.pattern = "(\[quote((.|\n)*?)\]((.|\n)*?)\[/quote\])"
      r.global = true
       r.ignorecase = true
      newcontent = r.replace(content, "<blockquote>&nbsp;<b>Quote: </b>&nbsp;(originally from $2)<br><br><div class=""quote"">$4</div></blockquote>")
      FormatQuote = newcontent
End Function
0
Comment
Question by:elhy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 14

Expert Comment

by:kiddanger
ID: 13558457
Is this what you're after?

<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = true

Function FormatQuote(content)
  dim r, newcontent, test
  set r = new regexp
  with r
    .Pattern = "\[quote\]"
    .global = True
    .ignorecase = True
  end with
  newcontent = r.Replace(content, "<blockquote>&nbsp;<b>Quote: </b>&nbsp;(originally from $2)<br /><br /><div class=""quote"">$4</div>")
  r.Pattern = "\[\/quote\]"
  newcontent = r.Replace(newcontent,"</blockquote>")
  FormatQuote = newcontent
  set r = nothing
End Function

dim f
f = formatQuote("[quote]testing [quote]123[/quote][/quote]")
Response.Write f
%>
0
 
LVL 5

Author Comment

by:elhy
ID: 13558603
well...ultimately after my posting I did it exactly the way you put it. But I am actually looking for 1 regexp to fix the nesting problem instead of having 2 regexp.
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13558664
well the way phpbb does it is they use what they call bbcode ... they actually go through the post once, looking for [quote] and others ... they add a uid to the end of the quote ... so like [quote:123D3]  and the corresponding [/quote:123D3]

then they use regexp to quote it.  But from what I've read ... you cannot do regular expressions with nested items like this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:elhy
ID: 13558866
it can be done with php but i haven't found a solution for ASP. I've read the post somewhere in EE, someone managed to get it work in php.
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13559139
how is it done in php?
0
 
LVL 10

Accepted Solution

by:
ThaSmartUno earned 2000 total points
ID: 13564658
<%
teststring = Request("teststring")
if teststring = "" Then teststring = "[quote=something]test[/quote]"

Set r = New RegExp
r.pattern = "\[quote=?(.*?)\](.*?)\[\/quote\]"
r.ignorecase = true

textarea = teststring
newTmp = ""

Do While (textarea <> newTmp)
    newTmp = textarea
    textarea = r.replace(textarea,"<span style=""font-weight:bold;""><br /><br />$1 wrote:</span><table border=""1"" width=""80%""><tr><td class=""some_style"">$2</td></tr></table>")
Loop
%>

<%=textarea%>
<form name="other" method=post>
<input type="text" name="teststring" value="<%=teststring%>" /><br/>
<input type="hidden" name="action" value="quote" />
<input type="submit" value="Quote this" />
</form>

using this ... it seems to work ...

using either
[quote=something]test[quote=something]test[/quote][/quote][quote=something]test[/quote]

[quote=something]test[/quote][quote=something]test[/quote]

but im not sure if there is anything else needed to test ... (this is just using the code from the php post)
0
 
LVL 5

Author Comment

by:elhy
ID: 13567105
this is damn cool!! Thanks! Can I ask for 1 more help? I'll create another thread for your points for this additional question if you can answer. as you can see, quote doesn't neccessary has a parameter meaning it can be [quote] or [quote=sth]. if it is [quote], I don't want the word "wrote: " to be there. How do I go about doing it?
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13567135
i did that too ... dont worry about another thread ... one sec
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13567156

Set r = New RegExp
r.pattern = "\[quote=?(.*?)\](.*?)\[\/quote\]"
r.ignorecase = true

textarea = teststring
newTmp = ""

Do
    newTmp = textarea
    textarea = r.replace(textarea,"<span style=""font-weight:bold;""><br /><br />$1 wrote:</span><table border=""1"" width=""80%""><tr><td class=""some_style"">$2</td></tr></table>")
Loop While (textarea <> newTmp)


r.pattern="\[quote\](.*?)\[/quote\]"
newTmp = ""

Do
    newTmp = textarea
    textarea = r.replace(textarea,"<table border=""1"" width=""80%""><tr><td class=""some_style"">$1</td></tr></table>")
Loop While (textarea <> newTmp)
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13567168
whoops ... change the first pattern to "\[quote=(.*?)\](.*?)\[\/quote\]"  (without the =? )
0
 
LVL 5

Author Comment

by:elhy
ID: 13567192
hmm...no way to do it in 1 regexp? this was what I used to do XD. Was thinking issit possible to do it within one regexp.
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13567211
oh ... well i dont know if you can use an "if" statement inside a regular expression ... i guess you may have to make that other topic =)
0
 
LVL 5

Author Comment

by:elhy
ID: 13567258
:D I have an idea that will work but I'm not sure if it is good because there are two loops...do you think efficiency will drop? here's the code, the rest are intact, I modified the do while loop only:

Do While (newcontent <> newTmp)
      newTmp= newcontent
      set matches = r.execute(newcontent)
      for each match_value in matches
            if match_value.submatches(0) = "" then
                  newcontent = replace(newcontent, match_value.value, "<blockquote>&nbsp;<b>Quote: </b><br><br><div class=""quote"">"& match_value.submatches(1) & "</div></blockquote>")
            else
                  newcontent = replace(newcontent, match_value.value, "<blockquote>&nbsp;<b>Quote: </b>&nbsp;(originally from " & match_value.submatches(0) & ")<br><br><div class=""quote"">" & match_value.submatches(1) & "</div></blockquote>")
            end if
      next
Loop
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13568679
well i think that would be either as efficient or maybe less efficient than 2 do while loops ...

i'm not sure though
0
 

Expert Comment

by:escapenm
ID: 25262965
I wanted to post a follow up to this (although the discussion is +4 years old. I found that the following pattern expression:

r.pattern = "\[quote=(.*?)\](.*?)\[\/quote\]"

was not working properly when carriage returns exist in the quote block. I found this expression to better handle it:

r.pattern = "\[quote=(.*?)\]([^\]]+)\[\/quote\]"

Hope this helps.
0
 

Expert Comment

by:escapenm
ID: 25264596
Please disregard my previous post. The :

r.pattern = "\[quote=(.*?)\](.*?)\[\/quote\]"

is correct. I just needed to do :

szText = Replace(szText, vbCrLf, "<BR>")

BEFORE the RegExp to get it to work under all conditions.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question