public static string GenerateOutboundFileName(string fileNamePattern, int serialKey) { var _regexPatternExtraction = new Regex(@"{(.*?)}"); var _regldateFormatPattern = new Regex(@"[dMyhmsf]"); StringBuilder outputName = new StringBuilder(); outputName.Append(fileNamePattern); var matches = _regexPatternExtraction.Matches(fileNamePattern);
foreach (Match match in matches) { var matchText = match.Groups[0].Value; if (matchText.Contains("X")) { int digitsCount = matchText.Length - 2; outputName.Replace(matchText, serialKey.ToString("D" + digitsCount)); } else if (_regldateFormatPattern.IsMatch(matchText)) { var dateFormat = matchText.TrimStart('{').TrimEnd('}'); outputName.Replace(matchText, DateTime.Now.ToString(dateFormat)); }
}
return outputName.ToString();
}
Monday, 11 March 2019
Regex pattern for replacing DateTime and Serial number
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment