MySQL REGEXP No Whitespaces No Numbers

五迷三道 提交于 2021-01-27 05:45:18

问题


I'm trying to return only those rows which colA and colB do not contain a number or a whitespace

Here is my code so far.

SELECT * FROM table_name WHERE colA REGEXP '[^0-9^\W]' AND colB REGEXP '[^0-9^\W]'

given the dataset

   colA        colB
---------- ----------
     test |  testB
      33  |  text    <- this is not returned
     blah |  0123A   <- this is returned

I am assuming my issue is with my regexp... any help please?


回答1:


Well your expression does not work because: 33 is both numbers and you're looking for any non decimal, non whitespace character. so it doesn't include that row in result.

Try:

SELECT * FROM table_name WHERE colA NOT REGEXP '[0-9[:space:]]' AND colB NOT REGEXP '[0-9[:space:]]'

ETA: Yea forgot that \whatever does not work




回答2:


MySQL doesn't support the \whatever type escapes for the most part. If you want whitespace, you need to use [[:space:]], which is POSIX syntax. Details on the regex man page here.




回答3:


id suggest to use negate instead: not regexp '[0-9[:space:]]'

SELECT * FROM table_name WHERE colA NOT REGEXP '[0-9[:space:]]' AND colB NOT REGEXP '[0-9[:space:]]'



回答4:


Another regex you can try is the following: regexp '[^0-9\s]'




回答5:


Not tested but give it a try

regexp '^[^0-9 ]+$'


来源:https://stackoverflow.com/questions/6988503/mysql-regexp-no-whitespaces-no-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!