Forum: Regexp'n bbcode

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
430e268ac31a3400cbaf4f9d797c837e?d=identicon&s=25 Mikko Heiskanen (morbusg)
on 2009-01-23 20:51
I'm scratching my head over the goal of converting bbcode to markdown.
The gist of the problem boils down to handling regexp matches inside
regexp matches.

Example: bbcode:
which I'd like to convert to:
> foo:
>> bar:
>> baz
> diu

What I've got so far is something like this:

  START = '\[quote'
  IDENT = '(?::\w+)?'
  USER = '(?:="([\w -_]+)")?'
  STOP = "\\[\\/quote#{IDENT}\\]"
  INSIDEQUOTE = /#{QUOTE}(.+?)(?!#{START})#{STOP}/m

def bbcode2markdown(txt)
  @quotedusers = {}
    txt.scan(/#{START}/).each_with_index { |match,index|
      txt.gsub!(INSIDEQUOTE) { |str| $2.gsub(/^(.)/, '>\1').gsub(/(.)$/,
"\n\\1\n") }
      depth = '>' * (index + 1)
      if $1
        @quotedusers[depth] = $1

    @quotedusers.each { |depth,user|
      txt.sub!(/^#{depth}(\w)/, "#{depth}**#{user}**:  \\1")

But I am missing some cases for some reason, and my brain is melting
merely at looking at that code after a couple of months after writing
I'm sure there must be a straight-forward way to get this done, but my
melted brain just can't figure out how.
This topic is locked and can not be replied to.