What's the best way to extract inner substrings from strings in Golang?
input:
"Hello <p> this is paragraph </p> this is junk <p> this is paragraph 2 </p> this is junk 2"
output:
"this is paragraph \n
this is paragraph 2"
Is there any string package/library for Go that already does something like this?
package main
import (
"fmt"
"strings"
)
func main() {
longString := "Hello world <p> this is paragraph </p> this is junk <p> this is paragraph 2 </p> this is junk 2"
newString := getInnerStrings("<p>", "</p>", longString)
fmt.Println(newString)
//output: this is paragraph \n
// this is paragraph 2
}
func getInnerStrings(start, end, str string) string {
//Brain Freeze
//Regex?
//Bytes Loop?
}
thanks
Don't use regular expressions to try and interpret HTML. Use a fully capable HTML tokenizer and parser.
I recommend you read this article on CodingHorror.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments