title: Matching '_' and '-' in java regexes
body: <p>I had this regex in <strong>java</strong> that matched either an alphanumeric character or the tilde (~)</p>
  
  <p><code>^([a-z0-9])+|~$</code></p>
  
  <p>Now I have to add also the characters <code>-</code> and <code>_</code> I've tried a few combinations, neither of which work, for example:</p>
  
  <p><code>^([a-zA-Z0-9_-])+|~$</code>
  <code>^([a-zA-Z0-9]|-|_)+|~$</code></p>
  
  <p>Sample input strings that must match:</p>
  
  <p>woZOQNVddd</p>
  
  <p>00000</p>
  
  <p>ncnW0mL14-</p>
  
  <p>dEowBO_Eu7</p>
  
  <p>7MyG4XqFz-</p>
  
  <p>A8ft-y6hDu
  ~</p>
  
  <p>Any clues / suggestion?</p>