如何使用正则表达式计算字符串中的单词

How do I Count the words in a string using regex

我正在尝试使用 Oracle 10g 中的正则表达式计算字符串中的单词数。 我一直在尝试这个

select *
from books
where REGEXP_LIKE(title, '[ ]{2}'); 

以便其返回标题中至少包含 3 个单词的标题。

REPLACE 完成工作(通过一些计算)。

SQL> with books as
  2    (select 'Tom Sawyer' title      from dual union all
  3     select 'A tale of two cities'  from dual union all
  4     select 'The Little Prince'     from dual union all
  5     select 'Don Quixote'           from dual
  6    )
  7  select title
  8  from books
  9  where length(title) - length(replace(title, ' ', '')) >= 2;

TITLE
--------------------
A tale of two cities
The Little Prince

SQL>

INSTR 也是一个可行的选择。通过查找第二次出现的 space,这将表明该字符串至少有 3 个单词。

WITH
    books
    AS
        (SELECT 'Tom Sawyer' title FROM DUAL
         UNION ALL
         SELECT 'A tale of two cities' FROM DUAL
         UNION ALL
         SELECT 'The Little Prince' FROM DUAL
         UNION ALL
         SELECT 'Don Quixote' FROM DUAL)
SELECT title
  FROM books
 WHERE instr(title, ' ', 1, 2) > 0;

如果您坚持使用正则表达式,则可以使用下面的正则表达式来查找包含 3 个或更多单词的书籍。

WITH
    books
    AS
        (SELECT 'Tom Sawyer' title FROM DUAL
         UNION ALL
         SELECT 'A tale of two cities' FROM DUAL
         UNION ALL
         SELECT 'The Little Prince' FROM DUAL
         UNION ALL
         SELECT 'Don Quixote' FROM DUAL)
SELECT title
  FROM books
 WHERE REGEXP_LIKE (title, '(\S+\s){2,}');

(感谢@Littlefoot 的书籍!)

下面的简单易懂(适用于 11g 及更高版本):

下面只是创建一些示例数据

create table books as
with tab as
(
    select 'Tom Sawyer' title from dual
    union all
    select 'A tale of two cities' from dual
    union all
    select 'The Little Prince' from dual
    union all
    select 'The_Little_Prince' from dual
    union all
    select 'Don Quixote' from dual
    union all
    select null from dual
)
select  title
from    tab;

以下是您获得至少包含 3 个单词的标题的解决方案

select  title 
from    books
where   regexp_count(title, '\w+') > 2

输出: