SELECT first_name, last_name
FROM employees
WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$');
以下查询返回名字为Steven或Stephen的员工的名字和姓氏(其中first_name以Ste开头,以en结尾,其间为v或ph)
是否存在与查询将返回Ste和en之间would not have
(v或ph)的所有内容相反的调用?
这样它会返回如下内容:
Stezen
斯泰伦
就像在NOT
前面放REGEXP_LIKE
一样简单吗?
答案 0 :(得分:1)
你需要这样的东西:
SELECT 'Match'
FROM dual
WHERE REGEXP_LIKE ('Steden', '^Ste[^(v|ph)]en$');
修改强>
这将排除任何两个(或更多)字母组合,但仍然允许“v
”:
SELECT 'Match'
FROM dual
WHERE REGEXP_LIKE ('Stephen', '^Ste[[:alpha:]]en$');
由于Oracle不支持预见功能,因此我必须同意其他人明确处理“v
”,方法是排除整个名称(字)或至少指定其名称确切的位置。
SELECT name
FROM WhateverTable
WHERE REGEXP_LIKE (name, '^Ste[[:alpha:]]en$') AND SUBSTR(name, 4, 1) <> 'v';
答案 1 :(得分:1)
两个选项:
REGEXP_LIKE
测试:一个正则表达式一般匹配;和一个用于排除无效的匹配。REGEXP_SUBSTR
来测试一般匹配并提取匹配的子组,然后测试是否应该将其排除。然后,第三个查询将查看如何通过使另一个包含匹配条件的表来扩展查询,并允许您构建和测试多个名称变体。
Oracle 11g R2架构设置:
CREATE TABLE tbl ( str ) AS
SELECT 'Stephen' FROM DUAL
UNION ALL SELECT 'Steven' FROM DUAL
UNION ALL SELECT 'Stepen' FROM DUAL
UNION ALL SELECT 'Steephen' FROM DUAL
UNION ALL SELECT 'Steeven' FROM DUAL
UNION ALL SELECT 'Steeven' FROM DUAL
UNION ALL SELECT 'Smith' FROM DUAL
UNION ALL SELECT 'Smithe' FROM DUAL
UNION ALL SELECT 'Smythe' FROM DUAL
UNION ALL SELECT 'Smythee' FROM DUAL;
CREATE TABLE exclusions ( prefix, exclusion, suffix ) AS
SELECT 'Ste', 'v|ph', 'en' FROM DUAL
UNION ALL SELECT 'Sm', 'ithe?|ythe', '' FROM DUAL;
查询1 :
SELECT str
FROM tbl
WHERE REGEXP_LIKE( str, '^Ste(\w+)en$' )
AND NOT REGEXP_LIKE( str, '^Ste(v|ph)en$' )
<强> Results 强>:
| STR |
|----------|
| Stepen |
| Steephen |
| Steeven |
| Steeven |
查询2 :
SELECT str
FROM (SELECT str,
REGEXP_SUBSTR( str, '^Ste(\w+)en$', 1, 1, NULL, 1 ) AS match
FROM tbl)
WHERE match IS NOT NULL
AND NOT REGEXP_LIKE( match, '^(v|ph)$' )
<强> Results 强>:
| STR |
|----------|
| Stepen |
| Steephen |
| Steeven |
| Steeven |
查询3 :
SELECT str
FROM tbl t
WHERE EXISTS ( SELECT 1
FROM exclusions e
WHERE REGEXP_LIKE( t.str, '^' || e.prefix || '(\w+)' || e.suffix || '$' )
AND NOT REGEXP_LIKE( t.str, '^' || e.prefix || '(' || e.exclusion || ')' || e.suffix || '$' )
)
<强> Results 强>:
| STR |
|----------|
| Stepen |
| Steephen |
| Steeven |
| Steeven |
| Smythee |
答案 2 :(得分:1)
MINUS
SELECT *
FROM employees
WHERE REGEXP_LIKE( first_name , '^Ste([[:alpha:]])+en$')
MINUS
SELECT *
FROM employees
WHERE REGEXP_LIKE( first_name , '^Ste(v|ph)en$');
这也是:
WITH t AS
( SELECT 'Stezen' first_name FROM dual
UNION ALL
SELECT 'Steven' FROM dual
UNION ALL
SELECT 'Stephen' FROM dual
)
SELECT *
FROM t
WHERE REGEXP_LIKE( first_name , '^Ste([[:alpha:]])+en$')
AND NOT REGEXP_LIKE( first_name , '^Ste(v|ph)en$');