title: C# regex for specific character or nothing
body: <p><strong>Short version</strong>:
  How do I match a single, specific character or nothing within a longer, potentially repeating, pattern?</p>
  
  <p><strong>Long version:</strong>
  I'm forming a regex to <strong>count the occurrences of string 'word</strong>' in strings which have the specific format of;
  a hyphen followed by an integer number (any length) followed by a hyphen followed by the string 'word' followed by a hyphen, potentially repeating.
  E.g.</p>
  
  <blockquote>
    <p>'-0-word-' (1 match)<br>
    '-10-word-' (1 match)<br>
    '-999-word-' (1 match)<br>
    '-1-word-1-word-' (2 matches)<br>
    '-1-word-1-word-222-word-' (3 matches) etc.</p>
  </blockquote>
  
  <p>If the pattern repeats then I think the leading hyphen has to be optional as it is already the trailing hyphen for the previous match.</p>
  
  <p>The best I have come up with so far is;</p>
  
  <pre><code>[-]?\d+-word-
  </code></pre>
  
  <p>which gives 3 matches for</p>
  
  <blockquote>
    <p>'-1-word-1-word-222-word-'</p>
  </blockquote>
  
  <p>but it also gives 3 matches for</p>
  
  <blockquote>
    <p>'-1-word-1-word-X222-word-'</p>
  </blockquote>
  
  <p>because the leading hyphen is optional and the 'X' is ignored. I want the leading hyphen to be only a hyphen or nothing. I want to make sure the whole string is rejected (no matches) if the format is not correct.</p>
  
  <p>Thanks for your help!</p>