I must admit, this is something that I’ve never done until just recently; creating a regular expression in Mysql. I’ve typically been able to accomplish whatever I wanted to with a LIKE statement or some server-side code.
However, just recently I updated the plugin I use to output sample code. I was previously using a syntax of [codesyntax lang=”php”] and now I needed to change it simply be [code]. This wasn’t that simple since I’ve done hundreds of blogs with several different languages. In searching I had over 15 distinct differences – hence, regular expressions to the rescue!
By default, Mysql has a built-in regular expression operator. Documentation is here: http://dev.mysql.com/doc/refman/5.1/en/regexp.html. This works nicely for your standard regular expressions, but it’s a bit lacking in functionality. A quick Google search brought me here: https://launchpad.net/mysql-udf-regexp. Now this is quite nice, it implements three different regular expression operations as UDFs (User Defined Functions): REGEXP_LIKE, REGEXP_SUBSTR, REGEXP_INSTR, and a fourth for actual replacement of data: REGEXP_REPLACE.
The neat part of these functions is they include an optional third and fourth parameter for the position and occurrence of the regular expression match. A nice addition when you only want to match to a specific occurrence of the string.
A nice simple regular expression as follows works nicely to grab them all:
[code]
(\[codesyntax)(.*?)(\])
[/code]
Now I can nice and quickly use the REGEXP_REPLACE function to change them all to [code].
If you are familiar with regular expressions in Oracle, these UDFs are meant to mimic them as closely as possible.