0

I have following dates:

@"August 25-27, 2013"

@"June 5-9, 2013"

@"May 20, 2014"

I need a regex which gives me the out put as follows:

@"August 25 2013"

@"June 5 2013"

@"May 20 2014"
Jumpei
  • 611
  • 5
  • 15
  • possible duplicate of [Regular Expression to match valid dates](http://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates) – jww Jan 14 '14 at 11:06

3 Answers3

2

Regex string

(\w{3,}) (\d+).*?(\d{4})

Replacement string

$1 $2 $3


Bonus!

Here is also an extremely specific version that will pick up only month names (including 3 letter month names ie. Jan,Feb,Mar,Nov) for the first bit.

(?<month>(?:Jan(:?uary)?)|(?:Feb(:?ruary)?)|(?:Mar(:?ch)?)|(?:Apr(:?il)?)|(?:May)|(?:Jun(:?e)?)|(?:Jul(:?y)?)|(?:Aug(:?ust)?)|(?:Sep(:?tember)?)|(?:Oct(:?ober)?)|(?:Nov(:?ember)?)|(?:Dec(:?ember)?)) (?<day>\d+).*?(?<year>\d{4})

It uses named captures so you would have the changed the replacement text to:

${month} ${day} ${year}

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
0

Regex matches are the substring of the original string, you won't be getting desired result in one match, you will be getting two matches and concatenation will yield the desired result. I would suggest to replace the unnecessary stuff with empty string. For that you can use:

((\-\d+)?,)
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0
echo "August 25-27, 2013" | sed -e "s/\\-[0-9]*//" -e "s/,//"

Output: August 25 2013

echo "June 5-9, 2013" | sed -e "s/\\-[0-9]*//" -e "s/,//"

Output: June 5 2013

echo "May 20, 2014" | sed -e "s/\\-[0-9]*//" -e "s/,//"

Output: May 20 2014

Have run on my os x 10.9

Damodaran
  • 10,882
  • 10
  • 60
  • 81