Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串C查找日期时间#_C#_Regex_Datetime - Fatal编程技术网

C# 从字符串C查找日期时间#

C# 从字符串C查找日期时间#,c#,regex,datetime,C#,Regex,Datetime,假设我有一个字符串,看起来像D1011608201313 第一部分是随机字母,中间部分是格式为dd/mm/yyyy的日期,梯形图是记录的id。然而,第一部分可以是非常随机的 像[Random String][DateTime][ID],我如何找到DateTime的位置。随机字符串的长度约为4到8个字符 如果我能找到datetime,它应该是非常直接的:)如果日期是DDMMYYYY格式,并且日期在1900-2099年的范围内,您可以使用这个正则表达式,但是可能会出现歧义。我还根据您在问题中的评论更

假设我有一个字符串,看起来像D1011608201313

第一部分是随机字母,中间部分是格式为dd/mm/yyyy的日期,梯形图是记录的id。然而,第一部分可以是非常随机的

像[Random String][DateTime][ID],我如何找到DateTime的位置。随机字符串的长度约为4到8个字符


如果我能找到datetime,它应该是非常直接的:)

如果日期是DDMMYYYY格式,并且日期在1900-2099年的范围内,您可以使用这个正则表达式,但是可能会出现歧义。我还根据您在问题中的评论更新了此内容,即日期是从当月开始的

public static void Main()
{ 
  // Leaves room for ambiguity if the random prefix or index suffix look 
  // like dates as well.
  var pattern = "((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))";

  // Or, I see in your comment that the dates are from the current month. 
  // If so then this decreases the probability of a false match. You could
  // use the following pattern instead:
  var year =  DateTime.Today.Year;
  var month  = string.Format("{0:00}", DateTime.Today.Month);
  pattern = "((0[1-9]|[12][0-9]|3[01])(" + month + ")(" + year + "))";        

  var str = "D1011608201313";
  var matches = Regex.Matches(str, pattern);
  if (matches.Count == 0) return;

  var groups = matches[0].Groups;     
  int d, m, y;
  int.TryParse(groups[2].Value, out d);
  int.TryParse(groups[3].Value, out m);
  int.TryParse(groups[4].Value, out y);
  var date = new DateTime(y, m, d);
  Console.WriteLine(date);
}
RegEx的详细分类(来自RegexBuddy):


如果事先至少知道ID,您可以保证通过正则表达式找到它

string result = Regex.Replace(source, @"^.*(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])(20[0-9][0-9])" + ID + @"$", "$1-$2-$3");

您可以在正则表达式捕获组中捕获字符串的各个部分,并分别引用它们

var matches = Regex.Matches("D1011608201313",@".*([0-9]{2})([0-9]{2})([0-9]{4}).{2}$");
if (matches.Count!=0)
{
    var match = matches[0];
    var year = Convert.ToInt32(match.Groups[3].Value);
    var month = Convert.ToInt32(match.Groups[2].Value);
    var day = Convert.ToInt32(match.Groups[1].Value);
    var result = new DateTime (year,month,day); 
}

你的身份证总是一样长吗?你不能从字符串的末尾开始吗?我猜id可以是任意数字?随机数据串可能包含数字?那么这是不可能的,因为记录id部分的长度总是相同的吗?这部分或第一部分可能必须是固定长度,以日期/时间分隔,否则需要有某种方法来区分它们(随机字符串是否总是字母?)。所有字符串是否都有21世纪的日期?是的,日期是从本月开始的。ID从一个数字到五个数字不等。请注意,这可能会在任何月份的第19天或第20天失败,随机字符串以数字结尾,这些数字恰好与日期类似。但是由于缺乏限制,你不能对此做任何事情。@Hans,没错,除非他事先知道每个案件的具体身份证,我认为这是不可能的。我们可以大大降低错误匹配的概率。我已经更新了我的答案。
var matches = Regex.Matches("D1011608201313",@".*([0-9]{2})([0-9]{2})([0-9]{4}).{2}$");
if (matches.Count!=0)
{
    var match = matches[0];
    var year = Convert.ToInt32(match.Groups[3].Value);
    var month = Convert.ToInt32(match.Groups[2].Value);
    var day = Convert.ToInt32(match.Groups[1].Value);
    var result = new DateTime (year,month,day); 
}