亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 FindAllString 正則表達式中排除重復項

在 FindAllString 正則表達式中排除重復項

Go
人到中年有點甜 2022-08-01 10:47:47
我有下面的代碼,即讀取根目錄中的所有文件,根據給定的單詞掃描它們以查找特定單詞,并報告每個文件中找到的單詞。我遇到的問題是,如果一個wod被提及的次數超過一個(即在文件中的不同位置),它將被報告與它出現一樣多,我需要將其重新排序為唯一的,因此排除重復項。例如,在文件中:txtregexregextxtI'm an engineer not a doctorreally, I'm not a doctor這個詞被報告兩次,而我需要讓它成為唯一的,即。它足以讓我知道它在文件中。我的代碼是:doctorpackage mainimport (    "fmt"    "io/ioutil"    "log"    "path/filepath"    "regexp"    "strings")func main() {    files, err := ioutil.ReadDir(".")    if err != nil {        log.Fatal(err)    }    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)    for _, file := range files {        if strings.ToLower(filepath.Ext(file.Name())) == ".txt" {            fmt.Println(file.Name())            b, err := ioutil.ReadFile(file.Name()) // just pass the file name            if err != nil {                fmt.Print(err)            }            //fmt.Println(b) // print the content as 'bytes'            str := string(b) // convert content to a 'string'            matches := re.FindAllString(str, -1)            fmt.Println(matches, len(matches))            for _, j := range matches {                fmt.Println(j)            }        }    }}
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

你可以從你的匹配從 a 轉換到 a 使用: https://godoc.org/github.com/golang-collections/collections/set[]stringSet

這樣:


package main


import (

    "fmt"

    "io/ioutil"

    "log"

    "path/filepath"

    "regexp"

    "strings"

    "github.com/golang-collections/collections/set"

)


func main() {

    files, err := ioutil.ReadDir(".")

    if err != nil {

        log.Fatal(err)

    }


    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)


    for _, file := range files {

        if strings.ToLower(filepath.Ext(file.Name())) == ".txt" {

            fmt.Println(file.Name())


            b, err := ioutil.ReadFile(file.Name()) // just pass the file name

            if err != nil {

                fmt.Print(err)

            }

            //fmt.Println(b) // print the content as 'bytes'


            str := string(b) // convert content to a 'string'

            matches := re.FindAllString(str, -1)

            matchesSet := set.New(matches...)

            matchesSet.Do(func print(s string) {

              fmt.Println(s)

            }

            // fmt.Println(matches, len(matches))


            // for _, j := range matches {

            //     fmt.Println(j)

            // }

        }

    }

}


查看完整回答
反對 回復 2022-08-01
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號