I am trying following code to extract quoted part from a string:
package main
import ("fmt")
func main(){
var oristr = "This is a \"test string\" for testing only"
var quotedstr = ""
var newstr = ""
var instring = false
fmt.Println(oristr)
for i,c := range oristr {
fmt.Printf("Char number: %d; char: %c
", i, c);
if c = `"` {
if instring
{instring=false}
else {instring=true}}
if instring
{quotedstr += c}
else {newstr += c}
}
fmt.Printf("Newstr: %s; quotedstr = %s", newstr, quotedstr )
}
However, I am getting following error:
# command-line-arguments
./getstring.go:11:14: syntax error: c = `"` used as value
./getstring.go:12:15: syntax error: unexpected newline, expecting { after if clause
./getstring.go:14:4: syntax error: unexpected else, expecting }
./getstring.go:15:3: syntax error: non-declaration statement outside function body
Why I am getting this error and how can this be corrected?
Also, is this approach all right or some other approach may be better?
This is the most basic way of getting what you want. It could be improved to be more robust etc.
package main
import (
"fmt"
"regexp"
)
func main() {
var oristr = "This is a \"test string\" for containing multiple \"test strings\" and another \"one\" here"
re := regexp.MustCompile(`"[^"]+"`)
newStrs := re.FindAllString(oristr, -1)
for _, s := range newStrs {
fmt.Println(s)
}
}