首页 > 解决方案 > Regex pattern not working in MySQL when I try to deny the dot

问题描述

I am trying to use a regex in MySQL. I have this expresión [a-zA-Z]^\\. I supossed the ^ symbol will deny the dot in the expression. If I write a text string with a dot I receive 0 matches but when I write a text string without the dot I also receive 0 matches. For example:

SELECT  'roger' REGEXP  '[a-zA-Z]^\\.' 

gives me 0 matches. I just need to verify if this expresión ^ denies the dot or not and what am I doing wrong. Thanks in advance.

标签: mysqlregex

解决方案


^ only means to exclude characters when it's at the beginning of a [...] character set. Outside that, it matches the beginning of the string.

If you want to match a non-dot, you should use [^.].

SELECT 'roger' REGEXP '[a-zA-Z][^.]'

But since this will match anywhere in the string, it won't work for excluding strings that contain .. Instead you should reverse the condition:

SELECT 'roger.' NOT REGEXP '[a-zA-Z]\\.'

推荐阅读