alexandere
alexandere

Reputation: 83

Matching a time string with a regular expression

I would like to match the time (10.00) from a string with the date and time ("21.01.08 10.00"). I'm using the following regular expression:

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}\\b" "g");

But this matches 21.01 from 21.01.08 and 10.00.

I'm using PCRE as my regualar expression engine.

Update:

I'm sorry, i should have more been more clear. The data and time are part of a larger string. I want to extract the time from that string.

For example:

"On 21.01.08 from 10.00 a party will take place in the library" "21.08.08 - At 10:00 there will be a party" "On 21.08.08 you are scheduled for a ... . The ... will begin at 10.00"

Is this possible?

Upvotes: 4

Views: 7036

Answers (3)

Michael Carman
Michael Carman

Reputation: 30831

Your original regex didn't work because \b (word boundary) matches at the "." in "21.01.08." You need to code the boundaries more robustly:

(?:[^\d:.]|^)(\d\d?[.:]\d\d)(?![.:\d])

This captures the time, in either of the notations you used, while excluding dates. Note that it does not validate the time. For example, it would match "88:99" Validating the time is possible but complicates the pattern significantly and is likely to be overkill for most situations.

It would be nice to use a look-behind instead of the non-capturing grouping but PCRE don't support variable-width look-behind.

Upvotes: 4

theraccoonbear
theraccoonbear

Reputation: 4337

^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$

should do the trick with the time part being put in a capture group.

the "new RegExp" I'm not sure about (Java perhaps?). In Perl you could get the value like...

if ("21.01.08 10.00" =~ m/^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$/g) {
  $time_part = $1;
}

in .NET the following should work...

  Regex r = new Regex(@"^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$");
  string dateTimeString = "21.01.08 10.00";
  if (r.IsMatch(dateTimeString)) {
    string timePart = r.Match(dateTimeString).Groups[1].Value;
    Console.Write(timePart);
  }
  Console.ReadKey();

You could also use a Named Capture if you want to use something less ambiguous then the index into the capture group.

Upvotes: 1

Pat
Pat

Reputation: 36702

try using

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}$" "g");

$ indicates end of string

Upvotes: 0

Related Questions