title: Java regular expression: A-Z and - or _, but only once
body: <p>I've only dabbled in regular expressions and was wondering if someone could help me make a Java regex, which matches a string with these qualities:</p>
  
  <ol>
  <li>It is 1-14 characters long</li>
  <li>It consists only of A-Z, a-z and the letters _ or -</li>
  <li>The symbol - and _ must be contained only once (together) and not at the start</li>
  </ol>
  
  <p>It should match</p>
  
  <ul>
  <li>Hello-Again</li>
  <li>ThisIsValid</li>
  <li>AlsoThis_</li>
  </ul>
  
  <p>but not</p>
  
  <ul>
  <li>-notvalid</li>
  <li>Not-Allowed-This</li>
  <li>Nor-This_thing</li>
  <li>VeryVeryLongStringIndeed</li>
  </ul>
  
  <p>I've tried the following regex string</p>
  
  <pre><code>[a-zA-Z^\\-_]+[\\-_]?[a-zA-Z^\\-_]*
  </code></pre>
  
  <p>and it seems to work. However, I'm not sure how to do the total character limiting part with this approach. I've also tried</p>
  
  <pre><code>[[a-zA-Z]+[\\-_]?[a-zA-Z]*]{1,14}
  </code></pre>
  
  <p>but it matches (for example) abc-cde_aa which it shouldn't.</p>