-2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Hipromark
  • 7
  • 1

1 Answers1

0

^ 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]\\.'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Sorry I thought that worked because it matches 1 for the string without the dot, but it also matches 1 for a string with a dot. Like this SELECT 'roger.' REGEXP '[a-zA-Z][^.]' so this solution does not deny the dot either – Hipromark Sep 21 '20 at 09:33
  • It matches anywhere in the string. So it matches `ro` because `[a-zA-Z]` matches `r` and `[^.]` matches `o`. – Barmar Sep 21 '20 at 12:24
  • I've updated the answer: you should use `NOT REGEXP` to exlude that. – Barmar Sep 21 '20 at 12:26
  • Thanks Barman. Your solution works fine and I did not know one can use not regexp, it is a good option. But I am still a little confused because I thought that using the ^ symbol inside an expression would exclude anything after it. – Hipromark Sep 22 '20 at 23:20
  • `^` has two meanings in regexp. At the beginning of `[...]` it means to exclude. Outside `[...]` it means to match the beginning of the string. – Barmar Sep 22 '20 at 23:31