SQL使用正则表达式REGEXP_LIKE()

时间:2016-02-12 13:59:29

标签: sql oracle regexp-like

我遇到了一些问题,我似乎无法像名字中那样匹配3个或更多个或者e的正则表达式。

找到管理员工的所有经理,他们的名字中至少有3个字母'a'或'e'(均为大写字母 和小写)。例如,名称中包含2'a'和1'e'将满足选择标准

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

当我这样做时,它会显示一个带有'e'或'a'的正确列表,但当我尝试做3或更多时,它会返回空白集。还提供了下面提供的样本数据。

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

样本数据

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

3 个答案:

答案 0 :(得分:1)

你正在寻找这个

(.*[ae]){3,}

.*接受那些想要的

之间的不同字符

所以你的查询变为:

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

i标志用于不敏感匹配,因此将资本AE考虑到......如果省略,则执行敏感匹配...

您也可以使用{3}代替{3,},在这种情况下会产生相同的结果

答案 1 :(得分:0)

如果您希望名称中的任意位置至少有3个ae

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

如果您想要至少连续3次ae,那么:

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

答案 2 :(得分:0)

You might try using def get_interesting_elems(L): for i,ele in enumerate(L): if condition(ele): yield ele print "%d/%d"%(i,len(L)) result=list(get_interesting_elems(my_list)) instead of REGEXP_COUNT():

REGEXP_LIKE()

The value of the third parameter in SELECT manager_name FROM manages WHERE REGEXP_COUNT(employee_name, '[ae]', 1, 'i') >= 3; indicates the position of where the search should start (in this case, we want to start at the beginning) while the REGEXP_COUNT() indicates that the search should be case-insensitive.

match_parameter 'i' was added with Oracle 11g; if you're using an earlier edition of Oracle then you can try the following:

REGEXP_COUNT()

The fourth parameter above (SELECT manager_name FROM manages WHERE REGEXP_INSTR(employee_name, '[ae]', 1, 3, 0, 'i') > 0; ) is the number of the occurrence of the regular expression.

or:

3

Either way it makes the regular expression simpler than if you use SELECT manager_name FROM manages WHERE REGEXP_SUBSTR(employee_name, '[ae]', 1, 3, 'i') IS NOT NULL; .