使用带有正则表达式的 mysql regex_replace [英] using mysql regex_replace with a regular expression

查看:36
本文介绍了使用带有正则表达式的 mysql regex_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此处找到的 regex_replace 函数http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/

I am using regex_replace function found here http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/

这个函数在 php 中运行良好,但似乎不喜欢 hash 的.我正在使用这种模式

This function works well in php but seem's not to like hash's. I am using this pattern

#^(0*)|([^\da-z])#i

在此处找到的正则表达式中如何删除所有非字母数字字符和前导零

in a regular expression found here How can I remove all non alpha numeric characters and leading zeros

我怎样才能在 mysql 中完成这项工作,无论是更改 mysql 函数还是调整表达式

How can i make this work in mysql either my changing the mysql function or adjusting the expression

推荐答案

像这样:

SELECT regex_replace('^0+|[^0-9a-zA-Z]','',sometextfield)

说明

  • 您尝试使用的 regex_replace 函数用于 MySQL,并且不像 PHP 那样使用分隔符,因此我们可以删除 #
  • 我们也可以去掉无用的括号
  • 0* 应该改成0+,否则就是替换空串
  • The regex_replace function you are trying to use is for MySQL and does not use delimiters like PHP, so we can remove the #
  • We can also remove the parentheses which serve no purpose
  • The 0* should be changed to 0+, otherwise we are replacing an empty string

正则表达式有什么作用?

  • ^ 锚断言我们在字符串的开头
  • 0+ 匹配一个或多个零
  • OR |
  • [^0-9a-zA-Z] 匹配一个既不是数字也不是字母的字符
  • The ^ anchor asserts that we are at the beginning of the string
  • 0+ matches one or more zeroes
  • OR |
  • [^0-9a-zA-Z] match a character that is neither a digit or a letter

这篇关于使用带有正则表达式的 mysql regex_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆