效果
源码
package main
import (
"fmt"
"regexp"
"strings"
"gitee.com/liumou_site/gbm"
)
func Grep(txt, sub string) []string {
var res string
re := "(.*)" + sub + "[^s]*"
c, err := regexp.Compile(re)
if err != nil {
panic(err)
}
TxtSp := strings.Split(txt, "\n")
for _, v := range TxtSp {
tmp := c.FindAllString(v, -1)
if len(tmp) > 0 {
res = res + tmp[0] + "\n"
}
}
r := strings.Split(res, "\n")
return gbm.SliceRemoveNull(r)
}
func main() {
txt := `Destination Gateway Genmask Flags Metric Ref Use Iface
default JDBoxV3.lan 0.0.0.0 UG 600 0 0 wlo1
10.1.1.0 0.0.0.0 255.255.255.0 U 600 0 0 wlo1
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-bb1893ff49af
172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-823a20cdeae2
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr1
`
r := Grep(txt, "172")
for i, v := range r {
fmt.Println(i, v)
}
}