匹配以 ([*>@]|--) 开头并以其中之一结尾的字符串

Match string beginning with ([*>@]|--) ending with one of these

我确实有一个像 *Task @Context >Delegation --Date 这样的字符串,我想提取分隔符 *@>-- 之间的字符串。 *Task @Context >Delegation --Date 应该产生四个字符串 TaskContextDelegationDate 以及 *Task 9-5 @Co-ntext >Dele-gation --Date 12-5 Task 9-5Co-ntext , Dele-gationDate 12-5.

matches = "*Task @Context >Delegation --Date" =~ /([\*@>]|--)([^\*@>\-]*)/

for (match in matches) {
    println "$match"
}

如果字符串不包含 -,效果很好,但每个字符串都可以包含一个(或多个)字符串。例如

matches = "*Task 9-5 @Co-ntext >Dele-gation --Date 12-5" =~ /([\*@>]|--)([^\*@>\-]*)/

for (match in matches) {
    println "$match"
}

所以,我尝试了负前瞻

matches = "*Task 9-5 @Co-ntext >Delegation A-Town --Date 12-5" =~ /([\*@>]|--)([^\*@>]*(?!--))/

for (match in matches) {
    println "$match"
}

但这不起作用。我尝试了无数种组合,但我不知道如何处理这两个 - 作为分隔符。

三种方法的输出:

First
[*Task , *, Task ]
[@Context , @, Context ]
[>Delegation , >, Delegation ]
[--Date, --, Date]
// is ok

Second
[*Task 9, *, Task 9]
[@Co, @, Co]
[>Dele, >, Dele]
[--Date 12, --, Date 12]
// problems with -

Third
[*Task 9-5 , *, Task 9-5 ]
[@Co-ntext , @, Co-ntext ]
[>Dele-gation --Date 12-5, >, Dele-gation --Date 12-5]
// problems with -

您可以在此处实施 Negative Lookahead。

def s = "*Task 9-5 @Co-ntext >Delegation A-Town --Date 12-5" 
def m = s =~ /([@>*]|--)((?:(?![*@>]|--).)*)/
(0..<m.count).each { print m[it][2].trim() + '\n' }

输出

Task 9-5
Co-ntext
Delegation A-Town
Date 12-5