简体   繁体   中英

using mysql regex_replace with a regular expression

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

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

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

Like this:

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

Explanation

  • 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

What does the regex do?

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM