-2

Input is 1/1/0001 12:00:00 AM. After the regex I would like out put to be 1/1/0001

can't get the regex to working. Any idea>

forgot to post my failed regex

([^\s]+)

By the way, I am looking for Date conversion. String to string conversion. The above results in 3 words seperated by ;

torres
  • 1,283
  • 8
  • 21
  • 30
  • 1
    I would be inclined to parse as a `DateTime` and then `ToString()` the date... – Russ Cam Apr 13 '13 at 11:06
  • @Russ, why not simply splitting the string on whitespace, then? – Frédéric Hamidi Apr 13 '13 at 11:09
  • 1
    There are many different ways to go about this (string split, regex, parsing via `DateTime` and more). However, we don't understand the exact requirements after seeing only a single example, we don't know how you are to use the result and most importantly, you have not shown any of your efforts to get to an answer yourself. – Oded Apr 13 '13 at 11:11
  • The regular expression line you posted wouldn't even compile - and why did you try that pattern? – Oded Apr 13 '13 at 11:16
  • @Oded because Copy/Paste is bad (: – iPath ツ Apr 13 '13 at 11:18
  • ([^\s]+) tried this. result is 3 string seperated by ; – torres Apr 13 '13 at 11:39
  • @torres Doesn't this <^\d{1,2}/\d{1,2}/\d{2,4}> work for you? – iPath ツ Apr 13 '13 at 11:55

4 Answers4

0

You can create DateTime object from this string, after it - to ise built in functions, like ToShortDate()

evgenyl
  • 7,837
  • 2
  • 27
  • 32
0

In your particular case the RegEx would be:

^\d{1,2}/\d{1,2}/\d{2,4}

** This is general regex string. You have to escape backslashes in C#/JavaScript etc.

This will match 1/1/0001 or 01/01/0001 etc. But will also match 56/56/9999! So after getting the date part you should do sanity checks.

But I would prefer to create DateTime object and to get the date part using DateTime's members.

UPDATE

If you use ([^\s]+) then you get three capturing groups:

  • 1/1/0001
  • 12:00:00
  • AM

So you have to extract the first capturing group, usually with index 1:

var datePart = matchObj.Groups[1].Value // Should be "1/1/0001"
iPath ツ
  • 2,468
  • 20
  • 31
  • this works. What would the syntax to get all the words in a string after the first space. For example, bobs nice house. So the result should be " nice house" without the quote. – torres Apr 13 '13 at 12:03
  • That's another question that you should ask ;) But please take time to read info/examples here: http://www.regular-expressions.info/ – iPath ツ Apr 13 '13 at 12:07
  • \s(.*) this is the asnwer. – torres Apr 13 '13 at 12:28
  • @torres be aware of Patashu's comment here: http://stackoverflow.com/a/15987800/1022219 – iPath ツ Apr 13 '13 at 12:32
0
string Date = "1/1/0001 12:00:00 AM";
string NewDate = Convert.ToDateTime(Date).ToShortDateString(); //note the output will be relate to local datetime format
string NewDate2 = Date.Substring(0, Date.IndexOf(' '));

Convert it to DateTime first, and then back to string by using ToShortDateString() or remove everything that is after the space char.

Lemur
  • 2,659
  • 4
  • 26
  • 41
0

This will be the regex for getting date from your input

"\\d{1}-\\d{1}-\\d{4}"
Jay.D
  • 21
  • 4