I need to digest some bbcode with a Ruby regular expression.
I have to delimit the elements with the match
command and use a regexp /pattern/m
to get rid of newlines.
For example, my bbcode in a string is:
s="[b]Title[/b] \n Article text \n [b]references[/b]"
Then I use match
to delimit the parts of the text, especially the Title and the References parts which are enclosed between [b]
and [/b]
:
t=s.match(/\[b\](.*)\[\/b\]/m)
I use (..)
syntax to catch a string in the regexp and I use \
to escape the special [
and ]
characters. /m
is to get rid of newlines in the string.
Then t[1]
contains:
"Title[/b] \n Artucle text \n [b]references"
instead of "Title"
. because the match doesn't stop at the first occurance of [/b]
. And t[2]
is nil instead of "References" for the same reason.
How can I delimit the text parts enclosed between the usual bbcode tags?