title: How do I find a variable set of 5 numbers qualified by surrounding underscores?
body: <p>I am pulling file names into a variable (<code>@[User::FileName]</code>) and attempting to extract the work order number (always 5 numbers with underscores on both sides) from that string. For example, a file name would look like - <code>"ABC_2017_DEF_9_12_GHI_35132_S5160.csv"</code>. I want result to return "35132". I have found examples of how to do it such as this <code>SUBSTRING(FileName,1,FINDSTRING(FileName,"_",1) - 1)</code> but the underscore will not always be in the same location.</p>
  
  <p>Is it possible to do this in the expression builder?</p>
  
  <h2>Answer:</h2>
  
  <pre><code>public void Main()
  {
      string strFilename = Dts.Variables["User::FileName"].Value.ToString();
      var RegexObj = new Regex(@"_([\d]{5})_");
      var match = RegexObj.Match(strFilename);
  
      if (match.Success)
      {
          Dts.Variables["User::WorkOrder"].Value = match.Groups[1].Value;
      }
      Dts.TaskResult = (int)ScriptResults.Success;
  }
  </code></pre>