-2

I'm trying to use regex to parse bbcode, so far I could make this regex working fine

if re.search("(\[b\])", m, re.IGNORECASE):
    r = re.compile(r"\[b\](?P<name>.*?)\[\/b\]", re.IGNORECASE)
    m = r.sub(r'<b>\1</b>', m)

But on this case where I need to use multiple regular parentheses to catch font's styles and the contents wrapped inside font bbcode, for example

[f color="#fff" ...]string[/f]

, I can't get it working right as the output always ends up like this

string</font>

Here's my regex code. I don't know what I'm doing incorrectly here..

if re.search("(\[f .*?\])", m, re.IGNORECASE):
    r = re.compile(r"\[f (?P<tag>.*?)\](?P<name>.*?)\[\/f\]", re.IGNORECASE)
    m = r.sub(r'<font \g<tag>>\g<name></font>', m)
Daniel Hyuuga
  • 446
  • 1
  • 5
  • 13

2 Answers2

1

Daniel, looking at your mockup code, you are looking for something like this:

result = re.sub(r"\[f ([^\]]*)\]([^\[]*)\[/[^\]]*\]", r"<font \1>\2</font>", subject)

Using [f color="#fff" ...]string[/f] as input, the output is <font color="#fff" ...>string</font>. Of course that is not valid html, but that is what your code is trying to do, and you can easily tweak it from here to make the replacement exactly how you like.

Explain the Regex

\[                       # '['
f                        # 'f '
(                        # group and capture to \1:
  [^\]]*                 #   any character except: '\]' (0 or more
                         #   times)
)                        # end of \1
\]                       # ']'
(                        # group and capture to \2:
  [^\[]*                 #   any character except: '\[' (0 or more
                         #   times)
)                        # end of \2
\[                       # '['
/                        # '/'
[^\]]*                   # any character except: '\]' (0 or more
                         # times)
\]                       # ']'
zx81
  • 41,100
  • 9
  • 89
  • 105
0

try to use this package https://pypi.python.org/pypi/bbcode

Writing the code yourself might not be a good idea.

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189