How to check if a string contain 2 same char in a row?

huangapple go评论118阅读模式
英文:

How to check if a string contain 2 same char in a row?

问题

我有一个正则表达式,像这样:

  1. REGEX_2_SAME_CHARACTER_IN_A_ROW = "^(?:(.)(?!\\))*$"

并且使用该正则表达式检查密码是否包含连续两个相同的字符:

  1. contain2SameCharacterInARow, err := regexp.MatchString(REGEX_2_SAME_CHARACTER_IN_A_ROW, password)

但是我得到了这个错误:

  1. error match regex 2 same char in a row: error parsing regexp: invalid or unsupported Perl syntax: `(?!`

我已经阅读了其他问题,其中使用了regexp.MustCompile,但我不知道如何处理或编写代码,有人可以帮我解决吗?

你可以在这里查看我用于验证密码的完整代码:
https://play.golang.com/p/5Fj4-UPvL8s

英文:

I have a regex like this

  1. REGEX_2_SAME_CHARACTER_IN_A_ROW = "^(?:(.)(?!\\))*$"

and check password with that regex if it contain 2 same character in a row

  1. contain2SameCharacterInARow, err := regexp.MatchString(REGEX_2_SAME_CHARACTER_IN_A_ROW, password)

but I get this error

  1. error match regex 2 same char in a row: error parsing regexp: invalid or unsupported Perl syntax: `(?!`

I have read other question that using regexp.MustCompile but I don't know how to handle or code it, is there anyone can help me with the solution?

Here you can check my full code for validate password
https://play.golang.com/p/5Fj4-UPvL8s

答案1

得分: 1

你不需要锚点、非捕获组或负向先行断言。只需匹配并捕获任何字符 ((.)),然后跟随它自己 (\\1)。

  1. REGEX_2_SAME_CHARACTER_IN_A_ROW = "(.)\\1"

但这引出了下一个问题:Go 正则表达式不支持反向引用,所以你需要找到另一种解决方案。一种解决方案是自己循环遍历字符串。

以下是一个使用简单循环的解决方案:

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. )
  7. func main() {
  8. fmt.Println(ValidatePassword("passsword01"))
  9. }
  10. func ContainsRepeatedChar(s string) bool {
  11. chars := strings.Split(s, "")
  12. char := chars[0]
  13. for i := 1; i < len(chars); i++ {
  14. if chars[i] == char {
  15. return true
  16. }
  17. char = chars[i]
  18. }
  19. return false
  20. }
  21. func ValidatePassword(password string) error {
  22. contain2SameCharacterInARow := ContainsRepeatedChar(password)
  23. if contain2SameCharacterInARow {
  24. fmt.Println("duplicated char")
  25. return errors.New("invalid password")
  26. }
  27. fmt.Println("all good")
  28. return nil
  29. }

希望对你有所帮助!

英文:

You don't need the anchor, the non-capturing group, nor the negative lookahead. Simply match and capture any character ((.)) followed by itself (\\1).

  1. REGEX_2_SAME_CHARACTER_IN_A_ROW = &quot;(.)\&quot;

But this brings us to the next problem: Go regexes do not support back references, so you need to find a different solution. One would be looping the string yourself.

Here's a solution with a simple loop:

  1. package main
  2. import (
  3. &quot;errors&quot;
  4. &quot;fmt&quot;
  5. &quot;strings&quot;
  6. )
  7. func main() {
  8. fmt.Println(ValidatePassword(&quot;passsword01&quot;))
  9. }
  10. func ContainsRepeatedChar(s string) bool {
  11. chars := strings.Split(s, &quot;&quot;)
  12. char := chars[0]
  13. for i := 1; i &lt; len(chars); i++ {
  14. if (chars[i] == char) {
  15. return true
  16. }
  17. char = chars[i]
  18. }
  19. return false
  20. }
  21. func ValidatePassword(password string) error {
  22. contain2SameCharacterInARow := ContainsRepeatedChar(password)
  23. if contain2SameCharacterInARow {
  24. fmt.Println(&quot;duplicated char&quot;)
  25. return errors.New(&quot;invalid password&quot;)
  26. }
  27. fmt.Println(&quot;all good&quot;)
  28. return nil
  29. }

huangapple
  • 本文由 发表于 2022年10月31日 15:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/74260158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定