我有以下字符串:字符串以分號分隔符正則表達式
; 2017-01-01; 2017-01-01; ; 2017-01-01
在字符串的開始,只有分號(;
),沒有空間和分號(;
)。而我的正則表達式[^;]+
無法正常工作。我怎樣才能從這個字符串中獲得5個值?
1 empty
2 data
3 data
4 empty
5 data
我有以下字符串:字符串以分號分隔符正則表達式
; 2017-01-01; 2017-01-01; ; 2017-01-01
在字符串的開始,只有分號(;
),沒有空間和分號(;
)。而我的正則表達式[^;]+
無法正常工作。我怎樣才能從這個字符串中獲得5個值?
1 empty
2 data
3 data
4 empty
5 data
您可以分別;
前添加一個空格,然後使用[^;]+
和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,輸出:
這個正則表達式
(?:^|[^;])(?:(?!;).)*(?=;|$)
將執行以下操作:
;
字符;
;
;
的一部分匹配的鑑於源文本
注:所述第一線與;
隨後沒有空格結束,用;
後跟一個空格
; 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
請幫助我們通過使您的問題可讀並格式化您的代碼來幫助您。 – Utkanos
請顯示代碼的相關部分。 –
如果第一項和第四項爲空,該怎麼辦?輸入爲空時,'TRIM'返回NULL。它會好嗎? –