捕获字母组并根据组中的数字重复它们特定次数

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

Capture letter group and repeat them certain times with the number in group

问题

我明白你的问题。你想将字符串转换为"RLFLFPAPAPA",即去掉数字并重复字母组。你的想法是使用正则表达式来捕获字母组,但是你在下面的代码中无法得到结果。你想知道如何解决这个问题,或者是否需要尝试其他方法。

在你的代码中,你已经定义了一个正则表达式(\d)\((\w+)\),用于匹配数字和括号中的字母组。要解决这个问题,你可以使用FindAllStringSubmatch函数来找到所有匹配的子字符串,并将其重复。

下面是修改后的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. func main() {
  9. fmt.Println(move("R2(LF)3(PA)"))
  10. }
  11. func move(s string) string {
  12. rgx := regexp.MustCompile(`(\d)\((\w+)\)`)
  13. matches := rgx.FindAllStringSubmatch(s, -1)
  14. for _, match := range matches {
  15. count, _ := strconv.Atoi(match[1])
  16. repeated := strings.Repeat(match[2], count)
  17. s = strings.Replace(s, match[0], repeated, 1)
  18. }
  19. return s
  20. }

这样,你就可以得到期望的结果"RLFLFPAPAPA"。希望能对你有所帮助!

英文:

I have string like: "R2(LF)3(PA)", and want to convert it to RLFLFPAPAPA (get rid of number and repeact the group of letters)

The idea come out is using regexp to capture the group, but I am stuck to get the result with code below, how can I solve that? Or I need to try other methods?

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. fmt.Println(move("R2(LF)3(PA)"))
  8. }
  9. func move(s string) string {
  10. rgx := regexp.MustCompile(`(\d)\((\w+)\)`)
  11. // code need here
  12. return s
  13. }

答案1

得分: 2

似乎Go没有一个带有回调函数的替换函数(这对我们来说是最实用的情况)。但我找到了这个实现方法

所以思路是将包含数字的第一个组转换为整数值。然后我们可以重复第二个组中的字母这个数字次数:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. func main() {
  9. fmt.Println(move("R2(LF)3(PA)"))
  10. }
  11. func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
  12. result := ""
  13. lastIndex := 0
  14. for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
  15. groups := []string{}
  16. for i := 0; i < len(v); i += 2 {
  17. groups = append(groups, str[v[i]:v[i+1]])
  18. }
  19. result += str[lastIndex:v[0]] + repl(groups)
  20. lastIndex = v[1]
  21. }
  22. return result + str[lastIndex:]
  23. }
  24. func move(s string) string {
  25. rgx := regexp.MustCompile(`(\d)\((\w+)\)`)
  26. return ReplaceAllStringSubmatchFunc(rgx, s, func(groups []string) string {
  27. // 将第一个组中的数字转换为整数值。
  28. nbrTimes, err := strconv.Atoi(groups[1])
  29. if err != nil {
  30. // 处理错误(如果数字很长可能会发生错误)。
  31. }
  32. // 返回第二个组中的字母重复n次。
  33. return strings.Repeat(groups[2], nbrTimes)
  34. })
  35. }

你可以在这里在线运行它:https://go.dev/play/p/FKNFJSc_tdS

英文:

It seems that Go doesn't have a replace function with a callback function (which is the most praticle case for us here). But I found this implementation to do it.

So the idea is to convert the first group containing a string with the number to an integer value. Then we can repeat the group 2 containing the letters this number of times:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;regexp&quot;
  5. &quot;strconv&quot;
  6. &quot;strings&quot;
  7. )
  8. func main() {
  9. fmt.Println(move(&quot;R2(LF)3(PA)&quot;))
  10. }
  11. func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
  12. result := &quot;&quot;
  13. lastIndex := 0
  14. for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
  15. groups := []string{}
  16. for i := 0; i &lt; len(v); i += 2 {
  17. groups = append(groups, str[v[i]:v[i+1]])
  18. }
  19. result += str[lastIndex:v[0]] + repl(groups)
  20. lastIndex = v[1]
  21. }
  22. return result + str[lastIndex:]
  23. }
  24. func move(s string) string {
  25. rgx := regexp.MustCompile(`(\d)\((\w+)\)`)
  26. return ReplaceAllStringSubmatchFunc(rgx, s, func(groups []string) string {
  27. // Convert the first group with the digits to an integer value.
  28. nbrTimes, err := strconv.Atoi(groups[1])
  29. if err != nil {
  30. // Handle error (could happen if very long number).
  31. }
  32. // Return the group 2 with the letters repeated n times.
  33. return strings.Repeat(groups[2], nbrTimes)
  34. })
  35. }

You can run it online here: https://go.dev/play/p/FKNFJSc_tdS

huangapple
  • 本文由 发表于 2022年9月26日 17:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/73851836.html
匿名

发表评论

匿名网友

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

确定