<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>theChrisWalker.net &#187; Regular Expressions</title>
	<atom:link href="http://thechriswalker.net/tag/regex/feed" rel="self" type="application/rss+xml" />
	<link>http://thechriswalker.net</link>
	<description>I like Web Development</description>
	<lastBuildDate>Tue, 31 Aug 2010 07:42:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Regular Expressions in MySQL</title>
		<link>http://thechriswalker.net/2009-01/regular-expressions-in-mysql.html</link>
		<comments>http://thechriswalker.net/2009-01/regular-expressions-in-mysql.html#comments</comments>
		<pubDate>Fri, 23 Jan 2009 09:40:16 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://thechriswalker.net/?p=21</guid>
		<description><![CDATA[I like regular expressions. Anyone who has tried to understand them will understand the learning curve involved and in no way do I claim to be an expert. However, I use them all the time. They are possibly the most useful thing I have ever learnt to do with programming and are more powerful I [...]]]></description>
			<content:encoded><![CDATA[<p>I like regular expressions. Anyone who has tried to understand them will understand the learning curve involved and in no way do I claim to be an expert. However, I use them all the time. They are possibly the most useful thing I have ever learnt to do with programming and are more powerful I imagined at first.</p>
<h3>So what does this have to do with MySQL?</h3>
<p>Well, I was recently dealing wit a table in which there is some inline JSON serialised data. This makes sense for the situation but of course if you want to query on something within the data you have to be a bit more careful. You have two options as I saw it yesterday:</p>
<ul>
<li>Pull all the data and parse it out outside of the database <em>- which seems weak and inefficient.</em></li>
<li>Use some LIKE query to pull likely candidates and then parse out of the data base <em>- again not so good, but a bit better.</em></li>
</ul>
<p><span id="more-21"></span></p>
<h3>The solution!</h3>
<p>You can use Regular Expressions in a MySQL query! To simplify the problem I faced, suppose you have a field in the database which contains JSON data like this:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;content&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">56</span><span style="color: #339933;">,</span><span style="color: #CC0000;">578</span><span style="color: #339933;">,</span><span style="color: #CC0000;">600</span><span style="color: #339933;">,</span><span style="color: #CC0000;">601</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;next&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">56</span> <span style="color: #009900;">&#125;</span></div></div>
<p>And I want to find all records which contain a specific value in the &#8220;content&#8221; array (and suppose I know which records will have the &#8220;content&#8221; array by another field/method). LIKE would not be enough as it leaves to much room for error, but Regular Expressions &#8211; easy! If I want to find the value 578 then:</p>
<div class="codecolorer-container mysql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`table`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`json<span style="color: #008080; font-weight: bold;">_</span>data`</span> <span style="color: #CC0099; font-weight: bold;">REGEXP</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\\</span>[([0-9]*,)*578(,|<span style="color: #004000; font-weight: bold;">\\</span>])'</span><span style="color: #000033;">;</span></div></div>
<p>Would find all records with 578 as an element in an array containing only numbers. Combine this with the method for restricting the SELECT to only filed you know will contain data in the right format and you have done it!</p>
<p>This is but one example, and the technique seems most useful when you have a serialised data format in your database field, but could be useful for advanced searching in table that do not support &#8220;fulltext&#8221; searches.</p>
]]></content:encoded>
			<wfw:commentRss>http://thechriswalker.net/2009-01/regular-expressions-in-mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
