C# 如何在c中根据日期分割字符串数据#
我有带日期的字符串,希望根据日期拆分为单独的字符串 例如:我有这种类型的字符串数据C# 如何在c中根据日期分割字符串数据#,c#,asp.net,C#,Asp.net,我有带日期的字符串,希望根据日期拆分为单独的字符串 例如:我有这种类型的字符串数据 "21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com\n21/04/21, 2:08 pm - Person2: this is second text\n21/04/21, 2:53 pm - Person3: Really a very us
"21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com\n21/04/21, 2:08 pm - Person2: this is second text\n21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n"
我想根据日期将其拆分,如
"21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com"
"21/04/21, 2:08 pm - Person2: this is second text"
"22/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n"
我试过下面的代码,但它是作为换行符而不是日期拆分的
string[] lines = text.Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None);
任何人都可以建议如何拆分此..正则表达式可能是您的解决方案
^[0-9]{2}/[0-9]{2}/[0-9]{2}[0-9]:[0-9]{2}(pm | am)
Regex regex = new Regex("^[0-9]{2}/[0-9]{2}/[0-9]{2}, [0-9]:[0-9]{2} (pm|am)", RegexOptions.Multiline);
var source = @"
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.
https://www.google.com
21/04/21, 2:08 pm - Person2: this is second text
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com";
var matches = regex.Matches(source);
foreach (Match match in matches)
{
var g = match.Groups[0];
Console.WriteLine($"{g.Index} : {g.Value}");
}
将产生以下输出,这是我的第一步建议:
2 : 21/04/21, 1:54 pm
109 : 21/04/21, 2:08 pm
159 : 21/04/21, 2:53 pm
我让你用sring spliting来完成它我想要一个完整的答案
static void Main(string[] args)
{
Regex regex = new Regex("^[0-9]{2}/[0-9]{2}/[0-9]{2}, [0-9]:[0-9]{2} (pm|am)", RegexOptions.Multiline);
var source = @"
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.
https://www.google.com
21/04/21, 2:08 pm - Person2: this is second text
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com";
var matches = regex.Matches(source);
foreach (Match match in matches)
{
var g = match.Groups[0];
Console.WriteLine($"{g.Index} : {g.Value}");
}
for (int i = 0; i < matches.Count - 1; i++)
{
int start = matches[i].Index;
int end = matches[i + 1].Index;
var item = source.Substring(start, end - start);
Console.WriteLine("---------");
Console.WriteLine(item);
}
var lastitem = source.Substring(matches.Last().Index);
Console.WriteLine("---------");
Console.WriteLine(lastitem);
}
string x=“21/04/21,下午1:54-Person1:了解这些产品真是一个非常有用的网站。\n\nhttps://www.google.com\n21/04/21,下午2:08-Person2:这是第二个文本\n21/04/21,下午2:53-Person3:这是一个非常有用的问答网站。\n\nhttps://www.stackoverflow.com\n”;
List lines=x.Split(新[]{“\r\n”,“\r”,“\n”},StringSplitOptions.None);
RemoveAll(s=>string.IsNullOrWhiteSpace(s));
对于(int i=0;i
这是否回答了您的问题?如果日期总是跟在“-”之后,那么就把它分开。”——“hypen出现在文本之间,也可以工作,但如果我尝试在12:08时返回,则不会返回预期结果。你能帮我在正则表达式中找到需要修改的地方吗。[0-9]+在正则表达式中添加了这个,以接受时间格式的2位数字
---------
21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.
https://www.google.com
---------
21/04/21, 2:08 pm - Person2: this is second text
---------
21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.
https://www.stackoverflow.com
string x = "21/04/21, 1:54 pm - Person1: Really a very useful website to know the products.\n\nhttps://www.google.com\n21/04/21, 2:08 pm - Person2: this is second text\n21/04/21, 2:53 pm - Person3: Really a very useful website for question and answers.\n\nhttps://www.stackoverflow.com\n";
List<string> lines = x.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
lines.RemoveAll(s => string.IsNullOrWhiteSpace(s));
for (int i=0; i < lines.Count(); i++)
{
if (lines[i].StartsWith("http"))
{
lines[i - 1] = lines[i - 1] + " " + lines[i];
lines.Remove(lines[i]);
}
}
for (int i = 0; i < lines.Count(); i++)
{
lines[i] = lines[i].Substring(lines[i].IndexOf(": ")).Remove(0,2);
Console.WriteLine(lines[i]);
}