title: Github username convention using regex
body: <p>I've been trying to convert the Github username convention using regex in Go for a while now and I couldn't do it. Also the username length <strong>shouldn't exceed more than 39 characters</strong>.</p>
  
  <p>Below is the username convention from Github</p>
  
  <blockquote>
    <p>Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.</p>
  </blockquote>
  
  <p>and for the length</p>
  
  <blockquote>
    <p>Username is too long (maximum is 39 characters). </p>
  </blockquote>
  
  <p>Here is the code I've written. You could check here in <a href="https://play.golang.org/p/PTu76U_512w" rel="nofollow noreferrer">Go playground</a></p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "regexp"
  )
  
  func main() {
      usernameConvention := "^[a-zA-Z0-9]*[-]?[a-zA-Z0-9]*$"
  
      if re, _ := regexp.Compile(usernameConvention); !re.MatchString("abc-abc") {
          fmt.Println("false")
      } else {
          fmt.Println("true")
      }
  }
  
  </code></pre>
  
  <p>Currently, I could achieve these:</p>
  
  <pre><code>a-b // true - Working!
  -ab // false - Working!
  ab- // false - Working!
  0-0 // true - Working!
  </code></pre>
  
  <p>But the problem I'm facing is that I couldn't find the regex pattern which should work for the below scenario:</p>
  
  <pre><code>a-b-c // false - Should be true
  </code></pre>
  
  <p>Also it has to be within 39 characters which I've found that we could use <code>{1,38}</code>, but I don't know where exactly should I add that in the regex pattern.</p>