使用golang解析Perl正则表达式

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

Parsing Perl regex with golang

问题

这是我的代码和代码示例。

  1. func insert_comma(input_num int) string {
  2. temp_str := strconv.Itoa(input_num)
  3. var validID = regexp.MustCompile(`\B(?=(\d{3})+$)`)
  4. return validID.ReplaceAllString(temp_str, ",")
  5. }
  6. func main() {
  7. fmt.Println(insert_comma(1000000000))
  8. }

基本上,我想要的输入是 1,000,000,000。
这个正则表达式在JavaScript中有效,但我不知道如何在Go中使用这个Perl正则表达式。非常感谢。谢谢!

英文:

http://play.golang.org/p/GM0SWo0qGs

This is my code and playground.

  1. func insert_comma(input_num int) string {
  2. temp_str := strconv.Itoa(input_num)
  3. var validID = regexp.MustCompile(`\B(?=(\d{3})+$)`)
  4. return validID.ReplaceAllString(temp_str, ",")
  5. }
  6. func main() {
  7. fmt.Println(insert_comma(1000000000))
  8. }

Basically, my desired input is 1,000,000,000.
And the regular expression works in Javascript but I do not know how to make this Perl regex work in Go. I would greatly appreciate it. Thanks,

答案1

得分: 2

由于前瞻断言似乎不被支持,我为您提供了一种不使用正则表达式的不同算法:

Perl 代码

  1. sub insert_comma {
  2. my $x=shift;
  3. my $l=length($x);
  4. for (my $i=$l%3==0?3:$l%3;$i<$l;$i+=3) {
  5. substr($x,$i++,0)=',';
  6. }
  7. return $x;
  8. }
  9. print insert_comma(1000000000);

Go 代码免责声明:我对 Go 语言没有任何经验,所以如果有错误,请谅解并随意编辑我的帖子!

  1. func insert_comma(input_num int) string {
  2. temp_str := strconv.Itoa(input_num)
  3. var result []string
  4. i := len(temp_str)%3;
  5. if i == 0 { i = 3 }
  6. for index,element := range strings.Split(temp_str, "") {
  7. if i == index {
  8. result = append(result, ",");
  9. i += 3;
  10. }
  11. result = append(result, element)
  12. }
  13. return strings.Join(result, "")
  14. }
  15. func main() {
  16. fmt.Println(insert_comma(1000000000))
  17. }

您可以在此链接中查看运行结果:http://play.golang.org/p/7pvo7-3G-s

英文:

Since lookahead assertion seems to be not supported, I'm providing you a different algorithm with no regexp:

Perl code:

  1. sub insert_comma {
  2. my $x=shift;
  3. my $l=length($x);
  4. for (my $i=$l%3==0?3:$l%3;$i&lt;$l;$i+=3) {
  5. substr($x,$i++,0)=&#39;,&#39;;
  6. }
  7. return $x;
  8. }
  9. print insert_comma(1000000000);

Go code: Disclaimer: I have zero experience with Go, so bear with me if I have errors and feel free to edit my post!

  1. func insert_comma(input_num int) string {
  2. temp_str := strconv.Itoa(input_num)
  3. var result []string
  4. i := len(temp_str)%3;
  5. if i == 0 { i = 3 }
  6. for index,element := range strings.Split(temp_str, &quot;&quot;) {
  7. if i == index {
  8. result = append(result, &quot;,&quot;);
  9. i += 3;
  10. }
  11. result = append(result, element)
  12. }
  13. return strings.Join(result, &quot;&quot;)
  14. }
  15. func main() {
  16. fmt.Println(insert_comma(1000000000))
  17. }

http://play.golang.org/p/7pvo7-3G-s

huangapple
  • 本文由 发表于 2013年9月28日 16:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/19065093.html
匿名

发表评论

匿名网友

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

确定