Search code examples
phpparsingtrim

Capture a date from an unknow string length


I have a string say "ETS 13-JUN-13sf342356". I am able to parse out the date with some simple ltrim and rtrim functions. My question is if I don't know what is before or after or how long the string is can I still capture this date? Some more examples are:

  • "JoeR13JUN134092883094"
  • "CC13-JUN-13tl320994"
  • "3425313-JUN-13tl345550"

Is there a single function that will always capture the (in this case) 13-JUN-13 date out of these strings?


Solution

  • I tried the following with your input and worked for that. Seems long but give it a try:

    ([0-9]{2})(\-[A-Z]+|[A-Z]+)(\-[0-9]{2}|[0-9]{2})