我有以下字符串:
; 2017-01-01; 2017-01-01; ; 2017-01-01
在字符串的开头,只有分号(;
),而不是空格和分号( ;
)。我的正则表达式[^;]+
无法正常工作。如何从此字符串中获取5个值?
1 empty
2 data
3 data
4 empty
5 data
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以在每个;
之前添加一个空格,然后使用[^;]+
和TRIM
每个项目:
SELECT rtrim(ltrim(REGEXP_SUBSTR(REPLACE(str, ';', ' ;'), '[^;]+', 1, LEVEL))) AS substr
FROM (
SELECT '; 2017-01-01; 2017-01-01; ; 2017-01-01' AS str FROM DUAL
)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(str, '[^;]+')) + 1
请参阅online demo,输出:
答案 2 :(得分:0)
这个正则表达式
(?:^|[^;])(?:(?!;).)*(?=;|$)
将执行以下操作:
;
字符;
开头,则;
;
作为比赛的一部分给出源文本
注意:第一行以;
结尾,后跟没有空格,第二行以;
结尾,后跟一个空格
; 2017-01-02; 2017-01-03; ; 2017-01-05;
; 2017-01-08; 2017-01-09; ; 2017-01-11;
返回以下内容
Match 1
Full match 0-0 ``
Match 2
Full match 1-12 ` 2017-01-02`
Match 3
Full match 13-24 ` 2017-01-03`
Match 4
Full match 25-26 ` `
Match 5
Full match 27-38 ` 2017-01-05`
Match 6
Full match 39-40 `
`
Match 7
Full match 40-40 ``
Match 8
Full match 41-52 ` 2017-01-08`
Match 9
Full match 53-64 ` 2017-01-09`
Match 10
Full match 65-66 ` `
Match 11
Full match 67-78 ` 2017-01-11`
Match 12
Full match 79-80 ` `
NODE EXPLANATION
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[^;] any character except: ';'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead