How do I pull 2 dates from a string variable?
I have a string with two dates included. I looked at a .Split and a .Substring function. Not sure I was going the most efficient route. Is there a clean way to pull the dates from the string?
string开发者_开发技巧 test = "Hello World. Random date1 is 12/10/2010. Now 4 days later is 12/14/2010."
If the date format will not change, you could do something like this:
private static readonly Regex dateRegex = new Regex(@"\d{1,2}/\d{1,2}/\d{4}");
public static IEnumerable<string> ExtractDates(string str)
{
return dateRegex.Matches(str).Cast<Match>().Select(match => match.Value);
}
Then use it like:
foreach (var dateString in ExtractDates("...")) {
// ...
}
Tested and working against your example string.
If it's actually alternatives you're interested in, and the string format does't change, you could:
string[] dates = test.Replace("Hello World. Random date1 is ", "").Replace("Now 4 days later is ", " ").Split(" ");
or just split on the text that doesn't change.
This is a tricky question. You could do something like this:
List<DateTime> dates = new List<DateTime>();
string test = "Hello World. Random date1 is 12/10/2010. Now 4 days later is 12/14/2010."
var potentialDates = test.Split(" .",StringSplitOptions.RemoveEmptyEntries);
foreach (string s in potentialDates)
{
DateTime d;
if (DateTime.TryParseExact(s, "MM/dd/yyyy", out d))
{
dates.Add(d);
}
}
This could probably be linq-ified, but it should work ok as is. As long as the dates are as indicated, this should work find. If you have time as well or if your date format includes '.'
, then you might want to modify the Split to only split on space. You could probably trim leading and trailing punctuation and be ok. Maybe something like this:
List<DateTime> dates = new List<DateTime>();
string test = "Hello World. Random date1 is 12/10/2010. Now 4 days later is 12/14/2010."
var potentialDates = test.Split(" ",StringSplitOptions.RemoveEmptyEntries);
foreach (string s in potentialDates)
{
DateTime d;
if (DateTime.TryParseExact(s.Trim(',', '.', ';', ':'), "MM/dd/yyyy", out d))
{
dates.Add(d);
}
}
A regular expression would be the most general way but, depending on the content around the dates, may be difficult to get right. It would potentially be very clean but could also be pretty ugly. On the plus side, you can guarantee that it's always going to be only a few lines of code and it's easy to change.
If your dates are always at specific indices in the string then you can use substring
, but if (for example) you could have Now 10 days later...
then that won't work very well.
Split
will work if you know that each date is always the nth word in the string.
EDIT: Just saw your comment about the text and format always being the same. I'd use split
or a regex like Hello world. Random date1 is (\d\d/\d\d/\d\d\d\d). Now \d+ days later is (\d\d/\d\d/\d\d\d\d)
. (I know, you could make that more concise. But it'll work. I think.)
EDIT2: A more concise regex after a bit more thought: .*?(\d{2}/\d{2}/\d{4}).*?(\d{2}/\d{2}/\d{4})
. This will let you change the surrounding text without needing to update the regex.
Use a Regular Expression
public void SampleRegexUsage()
{
string regex = @"\d{2}/\d{2}/\d{4}";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
string input = @"Hello World. Random date1 is 12/10/2010. Now 4 days later is 12/14/2010.";
MatchCollection matches = Regex.Matches(input, regex, options);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
Console.WriteLine(":" + match.Groups[""].Value);
}
}
精彩评论