带分号deimeter regexp的字符串

时间:2017-09-28 11:23:47

标签: regex

我有以下字符串:

; 2017-01-01; 2017-01-01; ; 2017-01-01

在字符串的开头,只有分号(;),而不是空格和分号( ;)。我的正则表达式[^;]+无法正常工作。如何从此字符串中获取5个值?

1 empty
2 data
3 data
4 empty
5 data

3 个答案:

答案 0 :(得分:1)

你应该使用正则表达式

[^;]*

请参阅regex101 demo

答案 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,输出:

enter image description here

答案 2 :(得分:0)

解决方案

这个正则表达式

(?:^|[^;])(?:(?!;).)*(?=;|$)

enter image description here

将执行以下操作:

  • 匹配由;字符
  • 分隔的所有字符串 如果源文本以; 开头,则
  • 匹配第一个空字符串 如果源文本以;
  • 结尾,则
  • 匹配最后一个空字符串
  • 不会返回;作为比赛的一部分

实施例

另见Live 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