Golang compiled regex to remove " and anything after and including @ in strings
10,642
One way you could do this based off your example input.
package main
import (
"fmt"
"regexp"
)
func main() {
s := `"Abraham Lincoln" @en`
reg := regexp.MustCompile(`"([^"]*)" *@en`)
res := reg.ReplaceAllString(s, "${1}")
fmt.Println(res) // Abraham Lincoln
}
If you have more data that follows the quotes, you could always change the expression to:
reg := regexp.MustCompile(`"([^"]*)".*@en`)
Author by
wordSmith
Updated on June 05, 2022Comments
-
wordSmith almost 2 years
If I have a string that looks like
"Abraham Lincoln" @en
. What i want to do is if it contains @en then remove the quotes, but keep what is inside and remove @en.What is the best way to do this in golang?
-
Avinash Raj over 9 yearsWhat's your expected output?
-
wordSmith over 9 years@AvinashRaj In that case
Abraham Lincoln
-
hwnd over 9 years@AvinashRaj I just finished a version of the regex explainer as a chrome extension.
-
hwnd over 9 yearsNot as of yet. I'll have to implement that next. If you use chrome, look at my profile the link and screenshot is in it.
-
Avinash Raj over 9 yearsok, i'll try that...
-