MidnightP
MidnightP

Reputation: 115

golang regular expression ReplaceAllString

I am writing a chatbot program in the Go programming language. In this function it reads in a user string checks for a regular expression and then is supposed to remove the expression and replace with another string if found. It is successful in finding a match but will not append it to the string

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

my output for response is "how do you know you are"

my intended output "how do you know you are feeling happy"

Upvotes: 2

Views: 3346

Answers (2)

Abdou
Abdou

Reputation: 13274

You can actually rewrite the regular expression to avoid having to loop. The following is an illustration of what @mypetlion is talking about:

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")
    }
}

The expression (?i)(i[' a]*m) (.*) basically captures two groups of characters present in the string. The first group is the various formats of I am. This should work for the other variants, as well. The second matches the remaining of the string after I am. Please note that we are using (?i) to make the regular expression case-insensitive.

Once we have compiled the expression, we move on to using the matched string from second group as our replacement.

You should get the following for all variations of I am:

output: How do you know you are feeling happy?

I hope this helps.

Upvotes: 3

mypetlion
mypetlion

Reputation: 2524

Your regular expressions are matching everything before and after the literals and extracting them. You need to actually capture the substring that will be used in your re.ReplaceAllString call. Replace the second .* in each expression with (.*).

Upvotes: 0

Related Questions