Search code examples
sqlregexsearchsqlyog

SQL REGEXP search ignoring '.'


I am trying to do a REGEXP search on everything in a database and I am trying to get all the .cxx file types. My current search is like

SELECT * FROM sourcefile 
WHERE idowner IS NULL AND name REGEXP '.cxx'

And the current output is something like

filecxx.txt 
file.cxx  
test.cxx

However I want to to only show the .cxx files not just the ones with cxx in their name so expected output would be

file.cxx  
test.cxx

Solution

  • REGEXP may return a partial match, it does not require a full string match.

    Thus, if you need to match a string ending with .cxx, you need

    AND name REGEXP '[.]cxx$'
    

    where [.] matches a literal . (if used as is, it matches any char) and $ matches the end of string.

    Or simply use LIKE

    AND name LIKE '%.cxx'