title: Trim string contents to get only the ID value
body: <p>I am trying to figure out how to trim a string so I get just the ID int value. I have a text box that can accept ID values so long as they are formatted like [@ID=#] 9. It can also accept BP [which stands for, "base price"] and valid numbers and math operators). What I need to do is get the number in that string and test to see if it is a valid ID. I have created a function that trims out the unnecessary characters and gets just the ID:</p>
  
  <pre><code>protected int CheckValidID(string idString)
  {
      bool IsValid = false;
      int ValidID = 0;
      String trimmedId = idString.Trim(new Char[] { ' ', '[', '@', 'I', 'D', '=', ']' });
  
      using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
      {
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = cn;
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "SELECT OptionChoiceId FROM ac_OptionChoices WHERE OptionChoiceId = @OptionChoiceID";
          cmd.Parameters.AddWithValue("@OptionChoiceID", trimmedId);
          cn.Open();
  
          using (IDataReader reader = cmd.ExecuteReader())
          {
              if (reader.Read())
                  if (reader["OptionChoiceId"].ToString() != "")
                  {
                      IsValid = true;
                      ValidID = Convert.ToInt32(reader["OptionChoiceId"]);
                  }
  
          }
          cn.Close();
      }
      return ValidID;
  }
  </code></pre>
  
  <p>If the ID passed in is valid, then I need to replace that ID with a number (any number). </p>
  
  <pre><code> if (PriceFormula.Text != "")
      {
          string pFormula = PriceFormula.Text;
          if (pFormula.Contains("@ID"))
          {
              CheckValidID(pFormula);
              if (CheckValidID(pFormula) != 0)
                  pFormula = pFormula.Replace(PriceFormula.Text, "2");
          }
          pFormula = pFormula.Replace("BP", "2");
          pFormula = pFormula.ToLower();      
          try
          {
              double n = 0;
              bool priceVal = (!Double.TryParse(ItemLookUp.evalExpression(pFormula).ToString(), out n));
          }
          catch (Exception x)
          {
              isValid = false;
              ErrorNote.Text += "This new option choice has an invalid Price Formula. &lt;br /&gt;";
          }
  
      }
  </code></pre>
  
  <p>The problem I have is if a user types in something like [@ID=#]*9, which is meant to be valid but doesn't work for what I have set up. Also, it doesn't work even if the user enters a valid ID. How can I set things up so that I can retrieve just the ID part of [@ID=#] regardless of whether it is typed into the text field on its own or with numbers and math operators?</p>
  
  <p>In the end, I need to test if what the user put in actually returns a valid result (in this case, a valid double). Either the ID or the BP text is replaced with a number (here I use 2) and that allows the formula to be tested. But it's the ID part that's got me stuck. </p>
  
  <p>Examples of VALID entries for the text field:</p>
  
  <ol>
  <li>[@ID=532134] </li>
  <li>[@ID=828718]*2</li>
  <li>[@ID=347479]*3+2</li>
  <li>BP*2</li>
  <li>BP+6</li>
  <li>BP*5+3</li>
  </ol>
  
  <p>Examples of INVALID entries:</p>
  
  <ol>
  <li>[ID=38288]</li>
  <li>@ID=383793  </li>
  <li>[@ID=39371]+ </li>
  <li>BP+  </li>
  <li>BP*2'  </li>
  <li>BP+9* </li>
  <li>BC*6</li>
  </ol>