public void ParsePattern(string line) { //Regex regex= new Regex(@"%-?[0-9]*\.?[0-9]*(?<pattern>[a-zA-Z]+)"); Regex regex = new Regex( @"(?<literal>[^%]*)%((?<min>-?[0-9]*)(\.(?<max>[0-9]*)))?(?<patternname>[a-zA-Z]+)(?<literal2>[^%]*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture); Match match = regex.Match(line); while (match.Success) { string literal = match.Groups["literal"].Value; if (false == String.IsNullOrEmpty(literal)) { elements.Add(new LiteralPatternLayoutElement(literal)); } if (match.Groups["patternname"].Success) { string patternName = match.Groups["patternname"].Value; int? minLength = null; int? maxLength = null; if (match.Groups["min"].Success) { minLength = int.Parse(match.Groups["min"].Value, CultureInfo.InvariantCulture); } if (match.Groups["max"].Success) { maxLength = int.Parse(match.Groups["max"].Value, CultureInfo.InvariantCulture); } ConversionPatternBase conversionPattern = FindLayoutElement(patternName); conversionPattern.MinLength = minLength; conversionPattern.MaxLength = maxLength; elements.Add(conversionPattern); //log.DebugFormat("{0} {1} {2} {3}", patternName, minLength, maxLength, literal); } string literal2 = match.Groups["literal2"].Value; if (false == String.IsNullOrEmpty(literal2)) { elements.Add(new LiteralPatternLayoutElement(literal2)); } match = match.NextMatch(); } }
private ConversionPatternBase FindLayoutElement(string patternName) { if (false == conversionPatternsMap.ContainsKey(patternName)) { throw new KeyNotFoundException( String.Format( CultureInfo.InvariantCulture, "The pattern '{0}' is not supported.", patternName)); } string className = conversionPatternsMap[patternName]; ConversionPatternBase patternLayoutElement = (ConversionPatternBase)Assembly.GetExecutingAssembly().CreateInstance(className); patternLayoutElement.PatternName = patternName; return(patternLayoutElement); }