一句话中的最后一个词:在 SQL 中(可以使用正则表达式吗?)

发布于 2024-09-27 11:19:54 字数 666 浏览 2 评论 0原文

我需要在 Oracle SQL (10gR2) 中完成此操作。但我想,我宁愿说得直白一点,任何好的、高效的算法都可以。

给定一行(或句子,包含一个或多个单词,英语),您将如何找到该句子的最后一个单词?

这是我在 SQL 中尝试过的。但是,我希望看到一种有效的方法来做到这一点。

select reverse(substr(reverse(&p_word_in)
                         , 0
                         , instr(reverse(&p_word_in), ' ')
                         )
                  )
      from dual;

这个想法是反转字符串,找到第一个出现的空格,检索子字符串并反转字符串。是不是效率很高呢?是否可以使用正则表达式?我使用的是 Oracle 10g R2。但我不介意看到其他编程语言的任何尝试,如果需要的话,我不介意编写 PL/SQL 函数。

更新:

杰弗里·坎普给出了一个精彩的答案。这非常有效。

回答

SELECT SUBSTR(&sentence, INSTR(&sentence,' ',-1) + 1)
FROM dual

I need this to be done in Oracle SQL (10gR2). But I guess, I would rather put it plainly, any good, efficient algorithm is fine.

Given a line (or sentence, containing one or many words, English), how will you find the last word of the sentence?

Here is what I have tried in SQL. But, I would like to see an efficient way of doing this.

select reverse(substr(reverse(&p_word_in)
                         , 0
                         , instr(reverse(&p_word_in), ' ')
                         )
                  )
      from dual;

The idea was to reverse the string, find the first occurring space, retrieve the substring and reverse the string. Is it quite efficient? Is a regular expression available? I am on Oracle 10g R2. But I dont mind seeing any attempt in other programming language, I wont mind writing a PL/SQL function if need be.

Update:

Jeffery Kemp has given a wonderful answer. This works perfectly.

Answer

SELECT SUBSTR(&sentence, INSTR(&sentence,' ',-1) + 1)
FROM dual

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜嗑 2024-10-04 11:19:54

我认为 INSTR/SUBSTR 更简单:

WITH q AS (SELECT 'abc def ghi' AS sentence FROM DUAL)
SELECT SUBSTR(sentence, INSTR(sentence,' ',-1) + 1)
FROM q;

I reckon it's simpler with INSTR/SUBSTR:

WITH q AS (SELECT 'abc def ghi' AS sentence FROM DUAL)
SELECT SUBSTR(sentence, INSTR(sentence,' ',-1) + 1)
FROM q;
苏大泽ㄣ 2024-10-04 11:19:54

不确定它的性能如何,但这应该可以做到:

select regexp_substr(&p_word_in, '\S+
) from dual;

Not sure how it is performance wise, but this should do it:

select regexp_substr(&p_word_in, '\S+
) from dual;
不一样的天空 2024-10-04 11:19:54

我不确定是否可以在 oracle 中使用正则表达式,但是

(\w+)\W*$

不起作用吗?

I'm not sure if you can use a regex in oracle, but wouldn't

(\w+)\W*$

work?

楠木可依 2024-10-04 11:19:54

这个正则表达式匹配一行中的最后一个单词:

\w+$

RegexBuddy 给出了在 Oracle 中使用的代码:

DECLARE
        match VARCHAR2(255);
BEGIN
        match := REGEXP_SUBSTR(subject, '[[:alnum:]]_+
, 1, 1, 'c');
END;

This regex matches the last word on a line:

\w+$

And RegexBuddy gives this code for use in Oracle:

DECLARE
        match VARCHAR2(255);
BEGIN
        match := REGEXP_SUBSTR(subject, '[[:alnum:]]_+
, 1, 1, 'c');
END;
油焖大侠 2024-10-04 11:19:54

这留下了标点符号,但得到了最终的词

with datam as (
SELECT 'abc asdb.' A FROM DUAL UNION
select 'ipso factum' a from dual union
select 'ipso factum' a from dual union
SELECT 'ipso factum2' A FROM DUAL UNION
SELECT 'ipso factum!' A FROM DUAL UNION
SELECT 'ipso factum  !' A FROM DUAL UNION
SELECT 'ipso factum/**//*/?.?' A FROM DUAL UNION
SELECT 'ipso factum  ...??!?!**' A FROM DUAL UNION
select 'ipso factum ..d.../.>' a from dual 

)
SELECT a,
--REGEXP_SUBSTR(A, '[[:alnum:]]_+
, 1, 1, 'c') , /** these are the other examples*/
--REGEXP_SUBSTR(A, '\S+
) ,  /** these are the other examples*/
regexp_substr(a, '[a-zA-Z]+[^a-zA-Z]*
)  

from datam

this leaves the punctuation but gets the final word

with datam as (
SELECT 'abc asdb.' A FROM DUAL UNION
select 'ipso factum' a from dual union
select 'ipso factum' a from dual union
SELECT 'ipso factum2' A FROM DUAL UNION
SELECT 'ipso factum!' A FROM DUAL UNION
SELECT 'ipso factum  !' A FROM DUAL UNION
SELECT 'ipso factum/**//*/?.?' A FROM DUAL UNION
SELECT 'ipso factum  ...??!?!**' A FROM DUAL UNION
select 'ipso factum ..d.../.>' a from dual 

)
SELECT a,
--REGEXP_SUBSTR(A, '[[:alnum:]]_+
, 1, 1, 'c') , /** these are the other examples*/
--REGEXP_SUBSTR(A, '\S+
) ,  /** these are the other examples*/
regexp_substr(a, '[a-zA-Z]+[^a-zA-Z]*
)  

from datam
一梦浮鱼 2024-10-04 11:19:54

即使对于非英语单词,这也有效:

SELECT REGEXP_SUBSTR ('San maria Calle Cáceres Numéro 25 principal izquierda, España', '[^ .]+
) FROM DUAL;

This works too, even for non english words :

SELECT REGEXP_SUBSTR ('San maria Calle Cáceres Numéro 25 principal izquierda, España', '[^ .]+
) FROM DUAL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文