gpt4 book ai didi

c# - 如何在c#中拆分带日期的字符串

转载 作者:行者123 更新时间:2023-11-30 13:46:48 24 4
gpt4 key购买 nike

我有带日期的字符串,我想用日期和字符串拆分它

例如 :我有这种类型的字符串数据

9/23/2013/marking abandoned based on notes below/DB
12/8/2012/I think the thid is string/SG

我想把它变成这样

9/23/2013     marking abandoned based on notes below/DB
12/8/2013 I think the thid is string/SG

所以,我不知道如何拆分这些字符串并存储在表的不同列中。请帮助我。

最佳答案

string[] vals = { "9/23/2013/marking abandoned based on notes below/DB",
"12/8/2012/I think the thid is string/SG" };

var regex = @"(\d{1,2}/\d{1,2}/\d{4})/(.*)";

var matches = vals.Select(val => Regex.Match(vals, regex));

foreach (var match in matches)
{
Console.WriteLine ("{0} {1}", match.Groups[1], match.Groups[2]);
}

打印:

9/23/2013     marking abandoned based on notes below/DB
12/8/2012 I think the thid is string/SG

(\d{1,2}/\d{1,2}/\d{4})/(.*) 分解为

  • (\d{1,2}/\d{1,2}/\d{4}):
    • \d{1,2} - 匹配任何一位或两位数字
    • / - 匹配一个 / 符号
    • \d{4} - 匹配四位数
    • (...) - 表示第一组
  • (.*) - 匹配所有其他内容并创建第二组

关于c# - 如何在c#中拆分带日期的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com