Rob*_*ith 5 oracle regexp-substr regexp-replace
我需要选择字符串中的前 X 个单词,其中 x 可以是 0-100 之间的任何数字。是否有捷径可寻?我找到了以下示例来从字符串中选择前 2 个单词:
select regexp_replace('Hello world this is a test', '(\w+ \w+).*$','\1') as first_two
from dual
Run Code Online (Sandbox Code Playgroud)
如何从字符串中选择前 X 个单词,其中 X 可以是 0-100 之间的数字?
选择前四个单词:
select
regexp_replace(
'Hello world this is a test etc',
'(((\w+)\s){4}).*', -- Change 4 to wanted number of words here!
'\1'
)
from dual;
Run Code Online (Sandbox Code Playgroud)
编辑
仅当单词仅由一个空格字符分隔时,上述解决方案才有效。如果单词由一个或多个空格字符分隔,则\s必须扩展为\s+:
select
regexp_replace(
'Hello world this is a test etc',
'(((\w+)\s+){4}).*', -- Change 4 to wanted number of words here!
'\1'
)
from dual;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11768 次 |
| 最近记录: |