REGEXP_SUBSTR 帮助

标签 regex oracle oracle10g

到目前为止,正则表达式是我的弱点。 我正在尝试分解以下字符串

Node 51 Path 1 Route 4
Node 51A Path 12 Route 3
Node 5 Path 12 Route 2
Node 7B Path 1 Route 1

我需要的是 Node 、 Node 的字母 、 Path 和 Route 。

我在提取节点的字母时遇到问题。节点的字母是单个非数字字符,始终跟在节点编号后面,中间没有空格。

第 2 行和第 4 行

Node 51A Path 12 Route 3 - Nodes letter is A
Node 5 Path 12 Route 2 - Nodes letter is NULL 
Node 7B Path 1 Route 1- Nodes letter is B

到目前为止,

with gen as (
    select 'Node 51 Path 1 Route 4' x from dual union all 
    select 'Node 51A Path 12 Route 3' x from dual union all 
    select 'Node 5 Path 12 Route 2' x from dual union all 
    select 'Node 7B Path 1 Route 1' x from dual
) 
select  x , 
        regexp_substr(x, '(\d+)',1,1) as Node , 
        regexp_substr(x, '(\d+)',1,2) as Path , 
        regexp_substr(x, '(\d+)',1,3) as Route
from    gen  

X                        NODE   PATH   ROUTE
------------------------ ------ ------ -------
Node 51 Path 1 Route 4   51     1      4
Node 51A Path 12 Route 3 51     12     3
Node 5 Path 12 Route 2   5      12     2
Node 7B Path 1 Route 1   7      1      1

Oracle 10gR2。

最佳答案

with gen as (
    select 'Node 51 Path 1 Route 4' x from dual union all 
    select 'Node 51A Path 12 Route 3' x from dual union all 
    select 'Node 5 Path 12 Route 2' x from dual union all 
    select 'Node 7B Path 1 Route 1' x from dual
) 
select  x , 
        regexp_substr(x, '\d+') as Node, 
        regexp_replace(regexp_substr(x, '\d+\S*'),'\d+') as NodeLetter , 
        regexp_substr(x, '\d+',1,2) as Path , 
        regexp_substr(x, '\d+',1,3) as Route
from    gen

关于REGEXP_SUBSTR 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426434/

相关文章:

database - Oracle -- 将数据导入到具有不同名称的表中?

mysql - linux 中用于处理数据库(oracle、mysql)的最佳 Gui 编辑器?

regex - 我想根据 Powershell 中的正则表达式为管道字符串的高亮部分着色

regex - 如何使用正则表达式删除文件开头的文本?

Java 8 正则表达式 : a capturing group in a pattern doesn't match, 但整个模式确实匹配

arrays - 在 PL-SQL 中循环之前缓存数组长度

python - 正则表达式从字符串中查找所有匹配项

Oracle 序列值未排序

SQL查询每天最新记录

oracle - 带引号的动态 sql 并立即执行