Skip to content

Instantly share code, notes, and snippets.

@Kubo2
Created July 4, 2017 14:28
Show Gist options
  • Save Kubo2/450e01b6abf0a317c32c95f19c9c4cb0 to your computer and use it in GitHub Desktop.
Save Kubo2/450e01b6abf0a317c32c95f19c9c4cb0 to your computer and use it in GitHub Desktop.
This is a pretty nice regexp for breaking down BBCode that I wrote a few yrs ago and just found it again in my old PC
# This regular expression is written by Kubo2
# It is designed to syntactily match BB Tags in
# raw BB Code.
#
# It is written specifically for dh Project BB parser core - DH\BB\Parser.
#
# Author: Kubo2 <kelerest123@gmail.com>
# Date: 11/17/2014
#---------------------------------------------------------------------------------------------
#
#
# match paired tags
# like [tag name some attrs]text content .*[/tag name]
\[ # opening brace
# tag name and attrs
#
(?<tag>
[a-zA-Z0-9]+ (?# tag name consists just of alphanumeric characters <del>or it is a breaker</del>)
)
(?<attrs>
[^\]]* # and does not contain any ] brace character
)
] # closing brace
(?<contents>
(?# tag contents - nested text)
(?# anything that DOES NOT contain end-tag)
.+?
(?<!\[/\k<tag>])
)
# stuff below is the endtag pattern
\[/\k<tag>]
#
# ---- OR ----
|
#
# match single-root tags
# with no end-tag
# [inline tag name attrs]
\[
(?<tag>
(?# tag name)
! |
\#\d+ |
\w+
)
]
#
# MUST NOT be followed by the closing tag
# but if that is, implies the first
Hi, this is some text, this is [b]bold text[/b]. Here is the fu[!]cking motherfuc[!]ker, and it behaves like a breaker.
New line in bb code [#2] suck
Fuck [b]this
one[/b]!
here then the horizontal line appears [hr]
Here is [url=http://example.com]link to example.com[/url] an some additional stuff
and fuck it all [url=hi]why does this suck?[/url]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment