Thursday, June 25, 2009

Regular Expression to search for time duration

How to find the following time string in a text file?
(09:00 -12:00)

Try regular expression:
\((\d{1,2}:\d{1,2}.*\d{1,2}:\w*)\)

Imagine you are trying to remove the time from this sting "meet up with friends (09:00 - 12:00)"

Code:
string result = System.Text.RegularExpressions.Regex.Replace("meet up with friends (09:00 - 12:00)", @"\((\d{1,2}:\d{1,2}.*\d{1,2}:\w*)\)",string.Empty)

Result:
result = "meet up with friends" is produced.

No comments: