我有下面的代碼,我希望用戶輸入一些關鍵字,然后從給定字符串中找到這些單詞中存在的內容,但生成的切片是長度等于要檢查的文本的空切片 playgroundmatchespackage mainimport ( "fmt" "regexp")func main() { p := []string{} p = append(p, "engineer") p = append(p, "doctor") var skills string for _, z := range p { skills += `|` + z } fmt.Println(skills) re := regexp.MustCompile(`(?i)` + skills) matches := re.FindAllString("I'm an engineer not a doctor", -1) fmt.Println(matches) for i, j := range matches { fmt.Println(i, j) }}
1 回答
POPMUISE
TA貢獻1765條經驗 獲得超5個贊
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
p := []string{}
p = append(p, "engineer")
p = append(p, "doctor")
p = append(p, "chemical (permit)")
skills := strings.Join(p, "|")
fmt.Println(skills)
re := regexp.MustCompile(`(?i)` + skills)
matches := re.FindAllString("I'm an engineer not a doctor who is getting chemical permits", -1)
fmt.Println(matches, len(matches))
for i, j := range matches {
fmt.Println(i, j)
}
}
輸出為:
engineer|doctor|chemical (permit)
[engineer doctor chemical permit] 3
0 engineer
1 doctor
2 chemical permit
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消
