如何从oracle plsql中的字符串中获取具有多个字母的倒数第二个单词

How to get the second last word which has more than one letters from a string in oracle plsql

我想从一个字符串中获取倒数第二个单词,该字符串中有一个以上的字母来自 plsql 中的一个字符串。

例如:-

如果字符串是 'John Smith A' 我需要得到 'Smith' 作为输出。

如果字符串是 'John Smith An' 我需要得到 'An' 作为输出。

我试过以下;它的正则表达式在正则表达式测试器中工作,但是当涉及到 oracle plsql 时,它没有给出任何输出。

SELECT regexp_substr('John Smith A', '([a-zA-Z]{2,}[^a-zA-Z]*)(?!.*[a-zA-Z]{2,}[^a-zA-Z])') FROM dual

你可以使用

SELECT regexp_substr('John Smith A', '([[:alpha:]]+)(\s+[[:alpha:]])?$', 1, 1, NULL, 1)

参见regex demo

模式匹配:

  • ([[:alpha:]]+) - 第 1 组:一个或多个字母
  • (\s+[[:alpha:]])? - 一个可选的序列
    • \s+ - 一个或多个空格
    • [[:alpha:]] - 一个字母
  • $ - 字符串结尾