<?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>Sniper &#187; Regular Expression</title>
	<atom:link href="http://tuncay.kinali.net/etiketler/regular-expression/feed" rel="self" type="application/rss+xml" />
	<link>http://tuncay.kinali.net</link>
	<description>Hafızamın bir kısmı</description>
	<lastBuildDate>Sun, 01 Aug 2010 09:49:05 +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>MySQL&#8217;de RegExp ile Replace Kullanımı</title>
		<link>http://tuncay.kinali.net/mysqlde-regexp-ile-replace-kullanimi.html</link>
		<comments>http://tuncay.kinali.net/mysqlde-regexp-ile-replace-kullanimi.html#comments</comments>
		<pubDate>Thu, 24 Sep 2009 17:34:57 +0000</pubDate>
		<dc:creator>Sniper</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[Regular Expression]]></category>
		<category><![CDATA[Replace]]></category>
		<category><![CDATA[Stored Function]]></category>

		<guid isPermaLink="false">http://tuncay.kinali.net/?p=215</guid>
		<description><![CDATA[Bir projemde MySQL&#8216;deki bir alanın değerinde RegExp kullanarak Replace işlemi yapma ihtiyacı duydum. Araştırdığımda MySQL&#8217;in Replace fonksiyonunun sadece string türünde parametre aldığını, dolayısıyla MySQL&#8217;in standart fonksiyonlarıyla bunu yapamayacağımı gördüm. Uzun zamandır Stored Procedure ve Stored Function konularını merak ettiğim halde hiç kullanma fırsatım olmamıştı. Bu durumu fırsat bilerek Regular Expression kullanarak değiştirme işlemi yapabileceğim bir [...]]]></description>
			<content:encoded><![CDATA[<p>Bir projemde <strong>MySQL</strong>&#8216;deki bir alanın değerinde <strong>RegExp</strong> kullanarak <strong>Replace</strong> işlemi yapma ihtiyacı duydum. Araştırdığımda MySQL&#8217;in Replace fonksiyonunun sadece string türünde parametre aldığını, dolayısıyla MySQL&#8217;in standart fonksiyonlarıyla bunu yapamayacağımı gördüm. Uzun zamandır <strong>Stored Procedure</strong> ve <strong>Stored Function</strong> konularını merak ettiğim halde hiç kullanma fırsatım olmamıştı. Bu durumu fırsat bilerek <strong>Regular Expression</strong> kullanarak değiştirme işlemi yapabileceğim bir Stored Function yazdım.<span id="more-215"></span></p>
<p>Hazırladığım fonksiyonun adı <strong>T_REGEXP_REPLACE</strong>, 3 parametre alıyor ve her üçü de VARCHAR veri tipinde. İlk parametrede değişikliğin uygulanacağı string, ikincisinde Regular Expression Deseni, üçüncüsünde ise değiştirilecek kısmın yerine konulacak karakter tanımlanır. Akılda kalıcı olsun diye kısaca şöyle de diyebiliriz: T_REGEXP_REPLACE(Neyi, Neye Göre, Neyle)</p>
<p>Aşağıdaki kodları <strong>MySQL Query Browser</strong> ya da muadili bir programla, kullanacağınız veritabanı üzerinde bir kez çalıştırıp fonksiyonu veritabanınıza ekleyin:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p215code3'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2153"><td class="code" id="p215code3"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">FUNCTION</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`test`</span>.<span style="color: #008000;">`T<span style="color: #008080; font-weight: bold;">_</span>REGEXP<span style="color: #008080; font-weight: bold;">_</span>REPLACE`</span>$$
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DEFINER</span><span style="color: #CC0099;">=</span><span style="color: #008000;">`root`</span>@<span style="color: #008000;">`localhost`</span> <span style="color: #990099; font-weight: bold;">FUNCTION</span> <span style="color: #008000;">`T<span style="color: #008080; font-weight: bold;">_</span>REGEXP<span style="color: #008080; font-weight: bold;">_</span>REPLACE`</span><span style="color: #FF00FF;">&#40;</span>t_string <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> character <span style="color: #990099; font-weight: bold;">set</span> utf8<span style="color: #000033;">,</span> t_pattern <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> t_replacement <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">RETURNS</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span> utf8
<span style="color: #990099; font-weight: bold;">BEGIN</span>
&nbsp;
	<span style="color: #808000; font-style: italic;">/* Değişkenleri tanımlayalım */</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> t_length<span style="color: #000033;">,</span> t_cursor<span style="color: #000033;">,</span> t_regex <span style="color: #999900; font-weight: bold;">TINYINT</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> t_result <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> character <span style="color: #990099; font-weight: bold;">set</span> utf8<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808000; font-style: italic;">/* Başlangıç değerlerini atayalım */</span>
	<span style="color: #990099; font-weight: bold;">SET</span> t_length <span style="color: #CC0099;">=</span> <span style="color: #000099;">CHAR_LENGTH</span><span style="color: #FF00FF;">&#40;</span>t_string<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> t_cursor <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> t_regex <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> t_result <span style="color: #CC0099;">=</span> <span style="color: #008000;">''</span><span style="color: #000033;">;</span>
&nbsp;
&nbsp;
	<span style="color: #808000; font-style: italic;">/* Tüm karakterleri tek tek pattern'imize tabi tutalım */</span>	
	WHILE t_length<span style="color: #CC0099;">&gt;-</span><span style="color: #008080;">1</span> <span style="color: #990099; font-weight: bold;">DO</span>		
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">SUBSTRING</span><span style="color: #FF00FF;">&#40;</span>t_string<span style="color: #000033;">,</span> t_cursor<span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">REGEXP</span> <span style="color: #990099; font-weight: bold;">BINARY</span> t_pattern <span style="color: #990099; font-weight: bold;">INTO</span> t_regex<span style="color: #000033;">;</span>
&nbsp;
		<span style="color: #009900;">IF</span> t_regex <span style="color: #009900;">THEN</span>
			<span style="color: #808000; font-style: italic;">/* Pattern'e uyan karakterleri t_replacement ile değiştirelim */</span>
			<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span>t_result<span style="color: #000033;">,</span> t_replacement<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> t_result<span style="color: #000033;">;</span>
		<span style="color: #009900;">ELSE</span>
			<span style="color: #808000; font-style: italic;">/* Uymayanları olduğu gibi bırakalım */</span>
			<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span>t_result<span style="color: #000033;">,</span> <span style="color: #000099;">SUBSTRING</span><span style="color: #FF00FF;">&#40;</span>t_string<span style="color: #000033;">,</span> t_cursor<span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> t_result<span style="color: #000033;">;</span>
		<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
		<span style="color: #990099; font-weight: bold;">SET</span> t_length <span style="color: #CC0099;">=</span> t_length<span style="color: #CC0099;">-</span><span style="color: #008080;">1</span><span style="color: #000033;">;</span>
		<span style="color: #990099; font-weight: bold;">SET</span> t_cursor <span style="color: #CC0099;">=</span> t_cursor<span style="color: #CC0099;">+</span><span style="color: #008080;">1</span><span style="color: #000033;">;</span>		
	<span style="color: #009900;">END</span> WHILE<span style="color: #000033;">;</span>
	RETURN<span style="color: #FF00FF;">&#40;</span>t_result<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Herhangi bir uyarı ya da hata mesajı almadıysanız ilk denemenizi yapabilirsiniz. Aşağıda &#8220;<strong>mysql</strong>&#8221; isimli veritabanının &#8220;<strong>help_category</strong>&#8221; tablosu üzerinde çalıştırılan sorgu ve çıktısını görebilirsiniz. Sorgu; <strong>help_category</strong> tablosunun <strong>name</strong> alanındaki değerlerde geçen tüm büyük harfleri <strong>&#8220;-&#8221;</strong> karakteri ile değiştirir.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p215code4'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2154"><td class="code" id="p215code4"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> name <span style="color: #990099; font-weight: bold;">AS</span> OriginalName<span style="color: #000033;">,</span> T_REGEXP_REPLACE<span style="color: #FF00FF;">&#40;</span>name<span style="color: #000033;">,</span> <span style="color: #008000;">'[A-Z]+'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'-'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> ReplacedName <span style="color: #990099; font-weight: bold;">FROM</span> help_category h <span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">0</span><span style="color: #000033;">,</span><span style="color: #008080;">10</span></pre></td></tr></table></div>

<p><a href="http://tuncay.kinali.net/wp-content/uploads/2009/09/mysql_ss.png" rel="lightbox[215]"><img src="http://tuncay.kinali.net/wp-content/uploads/2009/09/mysql_ss-300x222.png" alt="t_regexp_replace screenshot" title="t_regexp_replace screenshot" width="300" height="222" class="aligncenter size-medium wp-image-218" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://tuncay.kinali.net/mysqlde-regexp-ile-replace-kullanimi.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
