i`m very new in this…
i need a way to print data between ><img src="GRAB STRING " width="
like grab all data from between img src and width…and print it , tried using strings package no luck
If the text (or at least, each line of the text) contains at most one occurrence of img src="...", I would try a combination of strings.Index to find “src=” and strings.SplitN (with sep="\"" and n=1) to isolate the text between the double quotes.
If there are more occurrences of img src= in the same line, you could first split the text into individual HTML tags (with sep="<") and apply the above Index/SplitN to each of the resulting slices.
Another option are regular expressions, although a regexp can quickly become quite complex. (Shameless plug - here is a quick primer on regular expression that I posted a while ago.)
And finally, you could throw some HTML parser on the problem (a quick search on godoc.org retrieved golang.org/x/net/html as just one example. But this approach seems almost like using a sledgehammer to crack a nut.
As a suggestion, take the network out of the picture and practice solving a simpler version of the problem
package main
import (
"fmt"
)
func main() {
body := `<p>hello world <img src="http://example.com" /></p>`
img := body // here is where you parse body and extract the part you want
fmt.Println(img)
}
package main
import (
“fmt”
“strings”
)
func main() {
body := <p>hello world <img src="http://example.com" /></p>
// img := body // here is where you parse body and extract the part you want
split1 := strings.Split(body,<img src=")
split2 := strings.Split(split1[1], " />)
done := fmt.Sprintf("%s", split2[0])
fmt.Println(done)
}
problem is with strings.split i can only extract one line
I was going to say; the trick here is you cannot reasonably do this with string munging. You need some kind of xquery library to extract the element from the img tag.
Hint: You can make the code in your post look nicer if you indent ever line with four spaces. When you edit your post, the </> button does this for you.