golang正则表达式ReplaceAllString

时间:2017-11-16 00:40:00

标签: regex go chatbot

我正在使用Go编程语言编写一个chatbot程序。 在此函数中,它读入用户字符串检查正则表达式,然后应该删除表达式并替换为另一个字符串(如果找到)。它成功找到匹配但不会将其附加到字符串

input = "I am feeling happy"
pattern2 := []string{`.*i am.*`, `.*I AM.*`, `.*I'm.*`, `.*i'm.*`, `.*im.*`, `.*I am.*`}

// loop through pattern2 array
//if pattern is found extract substring
//set response

for _, checkPattern := range pattern2 {
    re := regexp.MustCompile(checkPattern)
    if re.MatchString(input) {
        match := re.ReplaceAllString(input, "How do you know you are $1 ?")
        response = "output : " + match
        return response
    } //if re.MatchString
} //for pattern2

我的回复输出是"你怎么知道你是"

我的预期输出"你怎么知道你感到快乐"

2 个答案:

答案 0 :(得分:3)

您实际上可以重写正则表达式以避免必须循环。以下是@mypetlion正在讨论的内容:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    input := "I AM feeling happy"
    re := regexp.MustCompile("(?i)(i[' a]*m) (.*)")
    if re.MatchString(input) {
        match := re.ReplaceAllString(input, "How do you know you are $2?")
        fmt.Println("output: " + match)
    } else {
        fmt.Println("There is no match")
    }
}

表达式(?i)(i[' a]*m) (.*)基本上捕获字符串中存在的两组字符。第一组是I am的各种格式。这也适用于其他变体。第二个匹配I am之后的剩余字符串。请注意,我们使用(?i)使正则表达式不区分大小写。

一旦我们编译了表达式,我们将继续使用 第二组 中的匹配字符串作为替代。

对于I am的所有变体,您应该获得以下内容:

output: How do you know you are feeling happy?

我希望这会有所帮助。

答案 1 :(得分:0)

您的正则表达式匹配文字前后的所有内容并将其解压缩。您需要实际捕获将在re.ReplaceAllString调用中使用的子字符串。将每个表达式中的第二个.*替换为(.*)