tags:

views:

1305

answers:

3

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?

A: 

try using

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

$ indicates end of string

Pat
+1  A: 
^\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.

theraccoonbear
I can't help but think that `\d\d` is as easy to read as `\d{2}`; it is even more economical on typing. If it gets above 3 (maybe above 2), or you need a range {3,7}, then the repeats are great.
Jonathan Leffler
Perhaps, I tend to do it out of habit and I also find that a firm numeric value is clearer than counting the number \d escape sequences. Just my personal preference I suppose.
theraccoonbear
+2  A: 

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.

Michael Carman