mysql与正则表达式
将comment表中的author_url包含www.sohu.com的记录,其中的sohu替换为sina,一个语句搞定~
update comment set author_url=REPLACE(author_url, 'sohu', 'sina') where author_url REGEXP "www.sohu.com";
带IF判断的复杂替换
update comment set url=IF(url REGEXP "test.yahoo.com.cn",REPLACE(url, 'www1.sohu.com', 'www.sina.com'),REPLACE(url, 'www2.yahoo.com', 'www.sina.com')) where 1=1;
相关文档(Relevant Entries)
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/4491
http://www.wujianrong.com/mt-tb.cgi/4491
评论 (1)
发现mysql的正则表达式很"另类"
今天碰到一个问题:
有一组整数用'|'隔开从小到大排列,现要确定某几个数是否在这个string写出mysql的正则表达式.
比如:
'|12|14|16|20|120|130|' 要确定12 16是否在这个string里面 .
最简单的办法是'.*|12|.*|16|.*',但是如果是'12' '14'呢?这个就不行了. '.*|12|.*|14|.*'是不会匹配前面的字符串的, 因为他们相邻,简单的解决办法是改造string用'||'分隔,但这样很浪费空间了.
看了半天manual发现一个很简单的方法'[[::]].*[[::]]' .
[[::]]匹配单词尾部
manual上:
[[::]]
These markers stand for word boundaries. They match the beginning and end of words, respectively. A word is a sequence of word characters that is not preceded by or followed by word characters. A word character is an alphanumeric character in the alnum class or an underscore (_).
同时'曾经未来'给出了一个比较通用的方法.'.*|12|(.*|)*14|.*'
正则表达式的确非常神奇阿.这个也算一题多解吧.
Posted by kevinwu | April 13, 2007 2:03 AM