c# Regular Expression for the text below? [closed]

This Regex should do it. The year is currently set to 4 digits, if you only want it to match 2005, replace the \d{4} bit.

^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$

Here’s the result:

   [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan] // matches
   [Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul] // no match
   [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20] // no match

Edit to your comment: Make sure you put an @ before the string declaration.

 var pattern = new Regex(@"^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$");
      var matches = pattern.IsMatch("[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]");

Leave a Comment