正则表达式匹配所有未被引号包围的事件并在字符处停止

Regex to match all occurances not surrounded by quote and stop at character

在欧洲已经很晚了,我无法得到正则表达式 运行 :-( 我想匹配所有文本,即 而不是 并用引号括起来。

输入:哦是的这个;输入;'with quotes';可以;'be very';棘手;'believe me';男孩

匹配所有用引号括起来的文本真的很容易。

DEMO

但我想要的恰恰相反。比赛也应该被分开;这样我就得到了以下匹配项:

  1. 哦对了这个
  2. 输入
  3. 可以
  4. 棘手
  5. 男孩

在我生气之前有什么想法吗?

提前谢谢!

我想你可以使用这样的技巧:

'[^;]*'(?=;|$)|([^;]+)(?=;|$)

解释:

'[^;]*'(?=;|$)      => finding words between `'` and ended with `;` or end of text, but not group it
|                   => or
([^;]+)(?=;|$)      => finding other words ended with `;` or end of text, but grouped

现在您可以使用 来捕捉您想要的东西了。

[ Regex Demo ]

您可以使用正则表达式

(?<=;|^)[^;'][^;]*[^;'](?=;|$)

查看 regex101 demo, regex storm demo