匹配但不包括使用正则表达式的结果

时间:2022-01-03 20:26:35

I am trying to match a group in regex but I don't want this group to be in the final result.

我正在尝试匹配正则表达式中的一个组,但我不希望这个组在最终结果中。

For example:

((kl(\.)?|at)?([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)

Running the above expression on at 12:25 should return 12:25.

在12:25运行上面的表达式应该返回12:25。

Is there any way to do this?

有没有办法做到这一点?

I tried using:

我试过用:

(?:((kl(\.)?|at)? )([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)

But that's no difference.

但这没什么区别。

Then I tried

然后我试了一下

(?<!(?:((kl(\.)?|at)? )([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)

But that returned an empty result.

但那返回了一个空洞的结果。

I am using the expression in C#.

我在C#中使用表达式。

1 个解决方案

#1


9  

A non-capturing group (not found in the match groups) is denoted as (?:). So,

非捕获组(在匹配组中找不到)表示为(?:)。所以,

(?:(?:kl(?:\.)?|at)?([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)

But you regexp seems to be wrongly structured from the outset. You don't capture the minutes.

但是你的regexp似乎从一开始就是错误的结构。你没有抓住会议记录。

#1


9  

A non-capturing group (not found in the match groups) is denoted as (?:). So,

非捕获组(在匹配组中找不到)表示为(?:)。所以,

(?:(?:kl(?:\.)?|at)?([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?)

But you regexp seems to be wrongly structured from the outset. You don't capture the minutes.

但是你的regexp似乎从一开始就是错误的结构。你没有抓住会议记录。