Github用户名约定使用正则表达式 [英] Github username convention using regex

查看:430
本文介绍了Github用户名约定使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试使用regex转换Github用户名约定,但我做不到.另外,用户名长度不能超过39个字符.

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 shouldn't exceed more than 39 characters.

下面是Github的用户名约定

Below is the username convention from Github

用户名只能包含字母数字字符或单个连字符,并且不能以连字符开头或结尾.

Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.

和长度

用户名太长(最多39个字符).

Username is too long (maximum is 39 characters).

这是我编写的代码.您可以在去游乐场

Here is the code I've written. You could check here in Go playground

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")
    }
}

目前,我可以实现以下目标:

Currently, I could achieve these:

a-b // true - Working!
-ab // false - Working!
ab- // false - Working!
0-0 // true - Working!

但是我面临的问题是我找不到适用于以下情况的正则表达式模式:

But the problem I'm facing is that I couldn't find the regex pattern which should work for the below scenario:

a-b-c // false - Should be true

此外,它必须在39个字符以内,我发现我们可以使用 {1,38} ,但是我不知道我应该在正则表达式模式中确切地添加该字符.

Also it has to be within 39 characters which I've found that we could use {1,38}, but I don't know where exactly should I add that in the regex pattern.

推荐答案

在基于Go RE2的正则表达式中,您不能使用环视功能,因此只能使用其他正则表达式或常规字符串长度检查来完成对长度限制的检查.

In Go RE2-based regex, you can't use lookarounds, so checking length limit can only be done either with another regex, or with regular string length checking.

完全非正则表达式的方法(演示):

A fully non-regex approach (demo):

package main

import (
    "fmt"
    "strings"
)
func IsAlnumOrHyphen(s string) bool {
    for _, r := range s {
        if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') && r != '-' {
            return false
        }
    }
    return true
}

func main() {
    s := "abc-abc-abc"
    if  len(s) < 40 && len(s) > 0 && !strings.HasPrefix(s, "-") && !strings.Contains(s, "--") && !strings.HasSuffix(s, "-") && IsAlnumOrHyphen(s) {
        fmt.Println("true")
    } else {

        fmt.Println("false")
    }
}

详细信息

  • len<40&&len>0 -长度限制,允许1到39个字符
  • !strings.HasPrefix(s,-")-不应以-
  • 开头
  • !strings.contains(s,-")-不应包含-
  • !strings.HasSuffix(s,-")-不应以-
  • 结尾
  • IsAlnumOrHyphen(s)-只能包含ASCII字母数字和连字符.
  • len(s) < 40 && len(s) > 0 - Length restriction, from 1 to 39 chars are allowed
  • !strings.HasPrefix(s, "-") - should not start with -
  • !strings.Contains(s, "--") - should not contain --
  • !strings.HasSuffix(s, "-") - should not end with -
  • IsAlnumOrHyphen(s) - can only contain ASCII alphanumeric and hyphens.

有关部分正则表达式的方法,请参见此Go演示:

For a partially regex approach, see this Go demo:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    usernameConvention := "^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$"
    re,_ := regexp.Compile(usernameConvention)
    s := "abc-abc-abc"
    if len(s) < 40 && len(s) > 0 && re.MatchString(s) {
        fmt.Println("true")
    } else {

        fmt.Println("false")
    }
}

在这里, ^ [a-zA-Z0-9] +(?:-[a-zA-Z0-9] +)* $ 正则表达式匹配

Here, the ^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$ regex matches

  • ^ -字符串的开头
  • [a-zA-Z0-9] + -1个或多个ASCII字母数字字符
  • (?:-[a-zA-Z0-9] +)* -0个或多个重复的-,然后是1个或多个ASCII字母数字字符
  • $ -字符串的结尾.
  • ^ - start of string
  • [a-zA-Z0-9]+ - 1 or more ASCII alphanumeric chars
  • (?:-[a-zA-Z0-9]+)* - 0 or more repetitions of - and then 1 or more ASCII alphanumeric chars
  • $ - end of string.

这篇关于Github用户名约定使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆