Search code examples
sqloracle-databaseregexp-like

SQL using regular expression REGEXP_LIKE()


I'm having some issues where I can't seem the like regex to match 3 or more a's or e's in the name.

Find all managers that manage employees with at least 3 letters 'a' or 'e' in their name (both uppercase and lowercase). For instance having 2 'a' and 1 'e' in the name, will satisfy the selection criteria

select manager_name
  from manages
 where regexp_like(employee_name, '[a,e]{3, }');

When I do this it shows a proper list with an 'e' or 'a' in it, but when I try to do 3 or more it returns blank set. Also sample data provided below.

select manager_name
  from manages
 where regexp_like(employee_name, '[a,e]');

Sample Data

William Gates III
Lakshmi Mittal
Ingvar Kamprad
Lawrence Ellison
Mark Zuckerberg
Sheryl Sandberg
Liliane Bettencourt
Michael Dell

Solution

  • You're looking for this instead

    (.*[ae]){3,}
    

    The .* accepts differents chars between those wanted

    So your query becomes:

    select manager_name
      from manages
      where 
      regexp_like(employee_name, '(.*[ae]){3,}', 'i');
    

    The i flag is for insensitive match, so capital AE are taken into account to... If ommitted, sensitive match is performed...

    You can also use simply {3} instead of {3,}, it will produce the same results in this case