Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match but don't include in result using regex

Tags:

c#

regex

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.

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#.

like image 230
simonbs Avatar asked Apr 17 '12 10:04

simonbs


1 Answers

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.

like image 170
Alexander Pavlov Avatar answered Sep 29 '22 19:09

Alexander Pavlov