在golang中的正则表达式匹配

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

Regex Matching in golang

问题

如何在字符串hello world中匹配字符串ello w

从尝试这个示例中得到了以下错误:

package main

import (
	"fmt"
	"regexp"
)

func check(result string) string {
	if regexp.MatchString(`b\ello w\b`, result) {
		fmt.Println("Found it")
		return "True"
	} else {
		return "False"
	}
}

func main() {
	text := "Hello world"
	check(text)
}

抛出以下错误:

# command-line-arguments
.\test.go:14:5: multiple-value regexp.MatchString() in single-value context
英文:

How do you match the string ello w in hello world

Got to this error from trying this example

package main 

import (
	"fmt"
	"regexp"

)

func check(result string  ) string {
	
	if (regexp.MatchString("b\\ello w\\b",result)) {
		fmt.Println("Found it ")
		return "True"
	} else {
		return "False"
	}
}

func main() {
	text := "Hello world "
    check (text)
	
} 

throws the following error

# command-line-arguments
.\test.go:14:5: multiple-value regexp.MatchString() in single-value context

答案1

得分: 2

regexp.MatchString 返回两个值。当你在 if 条件语句中使用它时,编译器会失败。

你应该先赋值返回值,然后处理错误情况,再处理匹配情况。

顺便说一下,你的正则表达式也有问题,请参考以下代码中的正确正则表达式:

func check(result string) string {
    // 错误的正则表达式
    // m, err := regexp.MatchString("b\\ello w\\b", result)
    m, err := regexp.MatchString("ello w", result)
    if err != nil {
        fmt.Println("你的正则表达式有问题")
        // 你应该记录日志或抛出错误
        return err.Error()
    }
    if m {
        fmt.Println("找到了")
        return "True"
    } else {
        return "False"
    }
}

func main() {
    text := "Hello world"
    check(text)
}

你可以在这里查看代码:https://play.golang.org/p/dNEsa9mIfhE

英文:

regexp.MatchString returns two value. When you use it in your if conditional, compiler fails.

You should assign the return values first, then handle error case and then the match case

By the way your regex was also faulty, please see the code for a correct one for your case

https://play.golang.org/p/dNEsa9mIfhE

func check(result string  ) string {
    // faulty regex   
    // m, err := regexp.MatchString("b\\ello w\\b",result)
    m, err := regexp.MatchString("ello w",result)
    if err != nil {
      fmt.Println("your regex is faulty")
      // you should log it or throw an error 
      return err.Error()
    }
    if (m) {
        fmt.Println("Found it ")
        return "True"
    } else {
        return "False"
    }
}

func main() {
    text := "Hello world "
    check(text)
} 

答案2

得分: 1

MatchString()返回两个值,一个是bool类型,一个是错误类型,所以你的if语句不知道如何处理这个。在下面的修正中,我只是丢弃了错误值,但我建议实际检查和处理错误。

package main

import (
    "fmt"
    "regexp"
)

func check(result string) string {
    found, _ := regexp.MatchString(`ello w`, result)
    if found {
        fmt.Println("找到了")
        return "True"
    } else {
        return "False"
    }
}

func main() {
    text := "Hello world"
    fmt.Println(check(text))
}

你可以在这里查看修正后的代码:https://play.golang.org/p/awAFxxAMyWl

英文:

MatchString() returns 2 values, a bool and an error, so your if statement doesn't know how to process that. https://pkg.go.dev/regexp#MatchString

In the correction below, I just through away the error value but I would recommend actually checking and handling the error.

https://play.golang.org/p/awAFxxAMyWl

package main 

import (
    "fmt"
    "regexp"

)

func check(result string  ) string {
    
found, _:= regexp.MatchString(`ello w`,result)    
if (found) {
        fmt.Println("Found it ")
        return "True"
    } else {
        return "False"
    }
}

func main() {
	
	text := "Hello world "

	fmt.Println(check(text))
    
} 

huangapple
  • 本文由 发表于 2021年10月28日 03:43:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/69744571.html
匿名

发表评论

匿名网友

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

确定