title: PHP regular expression allowing at most 1 '.' or '_' character in string, and '.' or '_' can't be at beginning or end of string
body: <p>I am writing a PHP validation for a user registration form. I have a function set up to validate a username which uses perl-compatible regular expressions. How can I edit it so that one of the requirements of the regular expression are AT MOST a single . or _ character, but NOT allow that character at the beginning or end of the string? So for example, things like "abc.d", "nicholas_smith", and "20z.e" would be valid, but things like "abcd.", "a_b.C", and "_nicholassmith" would all be invalid.</p>
  
  <p>This is what I currently have but it does not add in the requirements about . and _ characters.</p>
  
  <pre><code>function isUsernameValid()
  {
      if(preg_match("/^[A-Za-z0-9_\.]*(?=.{5,20}).*$/", $this-&gt;username))
      {
          return true; //Username is valid format
      }
      return false;
  }
  </code></pre>
  
  <p>Thank you for any help you may bring.</p>