RegEx表示可能包含分隔符的序列

时间:2018-04-26 15:58:00

标签: regex

我有一行文字,其中包含由

分隔的不同字符集

print()

space dash space (" - ")

我试图找到一个RegEx,它允许我在第三个破折号后抓取所有文本,即使它包含破折号。因此,对于aboce线,我想要捕获的文本将是..

005 - 0000 - 2310 - Storage - Summer
007 - 0000 - 1610 - Legal - Criminal Law
007 - 0000 - 8921 - Legal - Civil - Zoning
002 - 0000 - 2410 - Planning
123 - 0000 - 2510 - Finance/Corporate
008 - 0000 - 2520 - Legal - Technical Patents
321 - 0000 - 2610 - Clients & Storage

我想捕获的这篇文章可能包含任意数量的破折号。我只知道我要捕获的部分在第三个破折号后开始。另外,我不能指望所有数字组的长度都相同。如何定义RegEx以在第三个空格破折号空间后开始捕获所有内容?

我尝试了以下内容......

Storage - Summer
Legal - Criminal Law
Legal - Civil - Zoning
Planning
Finance/Corporate
Legal - Technical Patents
Clients & Storage

但是在第三个(.*) - (.*) - (.*)

之后它无法处理多个短划线

1 个答案:

答案 0 :(得分:1)

您可以使用^(?:.*?-){3}\s+(.*)$,它使用非贪婪匹配。

这将在第三个破折号之后的空格后捕获任何内容。

^                       // asserts position at start of line
(                       // start of group
  ?:                    // non-matching group
    .*?                 // matches any character lazily
    -                   // matches a -
){3}                    // matches 3 times
\s+                     // matches 1 or more whitespace
(                       // start of group 1
  .*                    // matches any character
)                       // end of group 1
$                       // asserts position at end of line