title: Regex compare a word with space before and after
body: <p>I have an array of string the store the user input in, and I want to check if the input user contains only the specific word <code>END</code> only, and I don't mind if there are any space before the word or after the word for example the user could input the word like <code>END</code> or "END   " or  "   END  " or "   END". I don't really care how many space there are before or after the word I just want to check that the input string contains ONLY the word <code>END</code> without taking the spaces into consideration.</p>
  
  <p>I tried </p>
  
  <pre><code>Regex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$");
  // to compare the word "END" only nothing before it nor after it - 
  // space is of anywhere before or after the word
  
  Match Char_Instruction_match = regex_ending_char.Match(Instruction_Separator[0]);
  if (!Char_Instruction_match.Success) // True if word doesn't end with "END"
  {
      richTextBox2.Text += "Error in line " + (LineNumber + 1) + ",  Code must end with 'END'" + Environment.NewLine;
  }`
  </code></pre>
  
  <p>I also tried </p>
  
  <pre><code>Regex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$"); 
  // to compare the word "END" only nothing before it nor after 
  // it - space is of anywhere before or after the word
  
  Regex.Replace(Instruction_Separator[0], @"\s+", "");
  Match Char_Instruction_match = regex_ending_char.Match(Instruction_Separator[0]);
  if (!Char_Instruction_match.Success) // True if word doesn't end with "END"
  {
      richTextBox2.Text += "Error in line " + (LineNumber + 1) + ",  Code must end with 'END'" + Environment.NewLine;
  }`
  </code></pre>
  
  <p>the problem is that I have to only check the first element of the array <code>Instruction_Separator[0]</code> and not any other element. So if the user inputs a space before the word <code>END</code> like so " END" then the <code>Instruction_Separator</code> array becomes <code>Instruction_Separator[0] = " ", Instruction_Separator[1] = END</code> and therefore there the code goes to the if condition even though the user input the correct string he only input a space at the beginning which I have no problem with if there is a space before or after the word.</p>
  
  <p>Thanks you everyone for your response, I respect all of your answers. What I'm trying to do is to build an assembler and I have to check for syntax error and comments are okay in the user input. SO for example if the user input was as follows:</p>
  
  <pre><code>ORG 100 //Begin at memory location 100
  LDA A // Load A
  A, DEC 83 // A has a decimal value of 83
  END // End the code
  </code></pre>
  
  <p>then there are aren't any syntax error and I could give a result.</p>
  
  <p>Also if the user added spaces before each line, that's fine as well</p>
  
  <pre><code>ORG 100 //Begin at memory location 100
      LDA A // Load A
      A, DEC 83 // A has a decimal value of 83
           END // End the code
  </code></pre>
  
  <p>so I want to check each line if it contains the correct syntax or not and I don't really care about any spaces before or after the correct format of each line.</p>
  
  <p>A user syntax error would be something like:</p>
  
  <pre><code>OR G 100 //Begin at memory location 100
  LDA A // Load A
  A, DEC 83 // A has a decimal value of 83
  EN  To end the code
  </code></pre>
  
  <p>notice that ORG is written "OR G" which is wrong, also END is written "EN" and the user forgot to place "//" before the comment "End the code"</p>
  
  <p>So What I need to do is to check that the last line contains the word "END" and if there is "//" then whats after it is a comment. But the user must type "//" if he wants to add a comment to a line. It's of if he doesn't want to add a comment it's not a must. Any idea how I can do this using regex as I mentioned above I tried <code>Regex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$");</code> but I didn't seem to work correct</p>
  
  <p>Thanks in advance looking forward for your replies.</p>