Link to home
Start Free TrialLog in
Avatar of lateapex
lateapex

asked on

How to pull date out of String

Experts,

Using C# I need to pull a date out of a string. The date format will always be numbers in day/month/year style. Of course the day and months could be 1 or 2 digits each, the years are always 4 digit. I've been reading about regex's but i can't seem to get it right. The string contains lots of words in addition to the date. The date always has at least one space leading and trailing it.
Example: "Here we 7/15/1957           down the path"

Thanks!
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

A pattern like this might work for you:
 ((?:3[0-2]|[0-2]?[1-9])\/(?:1[0-2]|0?[1-9])\/(?:\d{0,2}\d\d)) 

Open in new window

Assumptions:
To simplify the pattern, any month can have up to 31 days
Demo:
https://regex101.com/r/SkJcc2/2
I'm hoping you know how to put it into .NET code
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial