Commits

Anonymous committed 0f7618d

Refactoring Regex Pattern

Comments (0)

Files changed (1)

TemplateStrings/TemplateString.cs

             bool hasEndDelimiterEscape = !string.IsNullOrEmpty(EndDelimiterEscape);
             bool hasEndDelimiter = !string.IsNullOrEmpty(EndDelimiter);
 
-            if (hasStartDelimiterEscape && hasStartDelimiter)
+            if (hasStartDelimiterEscape || hasEndDelimiterEscape)
                 builder.Append('(');
 
             if (hasStartDelimiterEscape)
             {
-                builder.AppendFormat("(?<escapedstart>{0}){1}", Regex.Escape(StartDelimiterEscape),Regex.Escape(StartDelimiter));
+                builder.AppendFormat("(?<escapedstart>{0}{1})", Regex.Escape(StartDelimiterEscape), Regex.Escape(StartDelimiter));
+            }
+            else
+            {
+                builder.Append(Regex.Escape(StartDelimiter));
             }
 
-            if (hasStartDelimiter)
-            {
-                if (hasStartDelimiterEscape)
-                    builder.Append('|');
-
-                builder.AppendFormat("(?<start>{0})", Regex.Escape(StartDelimiter));
-            }
-
-            if (hasStartDelimiterEscape && hasStartDelimiter)
-                builder.Append(')');
-
-            if (hasIdPattern)
-                builder.AppendFormat("(?<named>{0})", IdPattern);
-
-            if (hasEndDelimiterEscape && hasEndDelimiter)
-                builder.Append('(');
+            builder.Append(IdPattern);
 
             if (hasEndDelimiterEscape)
             {
-                builder.AppendFormat("(?<escapedend>{1}){0}", Regex.Escape(EndDelimiterEscape),Regex.Escape(EndDelimiter));
+                builder.AppendFormat("(?<escapedend>{0}{1})", Regex.Escape(EndDelimiter), Regex.Escape(EndDelimiterEscape));
+            }
+            else
+            {
+                builder.Append(Regex.Escape(EndDelimiter));
             }
 
-            if (hasEndDelimiter)
-            {
-                if (hasEndDelimiterEscape)
-                    builder.Append('|');
+            if (hasStartDelimiterEscape || hasEndDelimiterEscape)
+                builder.Append(")|");
 
-                builder.AppendFormat("(?<end>{0})", Regex.Escape(EndDelimiter));
-            }
-
-            if (hasEndDelimiterEscape && hasEndDelimiter)
-                builder.Append(')');
+            builder.AppendFormat("({0}(?<named>{1}){2})", Regex.Escape(StartDelimiter), IdPattern, Regex.Escape(EndDelimiter));
 
             return builder.ToString();
         }