除单引号内的字符串外,正则表达式按空格分割

时间:2021-02-05 15:52:12

标签: regex

我有以下字符串

This is some testing dbo.GetPersonData(this_.PersonId,'Date contract recieved from the client') cs.dbo.Person 'this is test'

我想要它通用并获得以下结果

This
is
some
testing
dbo.GetPersonData(This_.PersonId,
'DateContract received from the client'
)
cs.dbo.person
'this is test'

我在这里想要实现的是用空格分隔单引号之间的任何内容

我试过使用

'(.*?)'|\S+

然而,它忽略了:<'Date contract recieved from the client'>

Result

3 个答案:

答案 0 :(得分:1)

<块引用>

我试过使用 '(.*?)'|\S+

你的尝试还不错,我们只需要从第二个选项中排除引用:

'.*?'|[^\s']+ regex101

答案 1 :(得分:1)

\S 匹配任何非空白字符,' 也匹配。

使用

'([^']*)'|[^\s']+

或者,如果您有 PCRE:

(?|'([^']*)'|([^\s']+))

proof

说明

--------------------------------------------------------------------------------
  '                        '\''
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  '                        '\''
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  [^\s']+                  any character except: whitespace (\n, \r,
                           \t, \f, and " "), ''' (1 or more times
                           (matching the most amount possible))

答案 2 :(得分:-1)

您应该使用以下模板:

(\n.*?)*)\n\s*\n