I have this string which is a result of net time \\SERVER_NAME command in cmd :
Current time at \\SERVER_NAME is 3/31/2014 9:35:57 AM
Obtaining a remote time of day...
If you're trying to talk to machines with multiple cultures - and if the culture of the system on which you're running may vary as well - I would strongly advise that you avoid this text-based approach.
Instead, consider using the NetRemoteTOD function. There may be a managed version of this, but if not you can use P/Invoke to call it - see the relevant pinvoke.net entry. This will allow you to get the appropriate value without any text handling, making things much cleaner.
Original answer (may be useful to others)
Well I would use DateTime.Parse
rather than a regular expression, after working out which part is the actual date/time. For example:
int startIndex = text.IndexOf(" is ") + 4;
DateTime dateTime = DateTime.Parse(text.Substring(startIndex));
That will get the value as a DateTime
- you can then format just the time part however you want, e.g.
string time24 = dateTime.ToString("HH:mm:ss"); "09:35:37"
string time12 = dateTime.ToString("h:mm:ss tt"); "9:35:37 AM"
Note that this is very likely to be culture-sensitive; the code may well not work in other cultures. If you want to make this more reliable, I would stop using net time
and instead use an API to talk to the server - I would really hope there'd be a way of doing this without using text handling...
If you really, really just want "whatever the part of the original string is after the date" then yes, you could use a regular expression - but I think it would be better to parse the value as a date/time to make sure it's genuinely the relevant data.
The simplest regex to only match the time would be something like:
\d\d?\:\d\d\:\d\d\s?(A|P)M
Is that what you're after?