duanfu6160
2018-03-18 18:45
浏览 122
已采纳

Golang正则表达式始终返回false?

I am taking a user input(a regular expression), and checking to see if a given line of a file would match it. I then return some ID if there's a match(the ID of the line), and that's about it. However, it appears as my match always returns false? But, interestingly, if I throw a wildcard .*, the program will take significantly longer to execute than a specific regular expression. So, there must be something going on -- why does it always return false?

Sample code:

func main() {

    // User input from command line
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter regexp: ")
    userRegexp, _ := reader.ReadString('
')

    // List all .html files in static dir
    files, err := filepath.Glob("static/*.html")
    if err != nil {
        log.Fatal(err)
    }

    // Empty array of int64's to be returned with matching results
    var lineIdArr []int64

    for _, file := range files {
        htmlFile, _ := os.Open(file)
        fscanner := bufio.NewScanner(htmlFile)

        // Loop over each line
        for fscanner.Scan() {

            line := fscanner.Text()

            match := matchLineByValue(userRegexp, line) // This is always false?

            // ID is always the first item. Seperate by ":" and cast it to int64.
            lineIdStr := line[:strings.IndexByte(line, ':')]
            lineIdInt, err := strconv.ParseInt(lineIdStr, 10, 64)

            if err != nil {
                panic(err)
            }

            // If matched, append ID to lineIdArr
            if match {
                lineIdArr = append(lineIdArr, lineIdInt)
            }
        }
    }
    fmt.Println("Return array: ", lineIdArr)
    fmt.Println("Using regular expression: ", userRegexp)
}

func matchLineByValue(re string, s string) bool {
    return regexp.MustCompile(re).MatchString(s)
}

is regexp.MustCompile(re).MatchString(s) not the right way to construct a regular expression from user input and match it to a whole line?

The string it matches is fairly long(it's basically a whole html file), would that present an issue?

图片转代码服务由CSDN问答提供 功能建议

我正在接受用户输入(正则表达式),并检查文件的给定行是否会 匹配它。 然后,如果匹配(行的ID),就返回一些ID,仅此而已。 但是,它显示为我的 match 总是返回false吗? 但是,有趣的是,如果我抛出通配符。* ,则该程序的执行时间将比特定的正则表达式长得多。 因此,必须发生某种情况-为什么它总是返回false?

示例代码:

  func main(){  
 
 //从命令行输入用户
 reader:= bufio.NewReader(os.Stdin)
 fmt.Print(“ Enter regexp:”)
 userRegexp,_:= reader.ReadString('
  ')
 
 //列出静态dir 
文件中的所有.html文件,err:= filepath.Glob(“ static / *。html”)
 if err!= nil {
 log.Fatal(err  )
} 
 
 //要返回具有匹配结果的int64的空数组
 var lineIdArr [] int64 
 
表示_,文件:=范围文件{
 htmlFile,_:= os.Open  (文件)
 fscanner:= bufio.NewScanner(htmlFile)
 
 //遍历每行
 for fscanner.Scan(){
 
行:= fscanner.Text()
 
匹配 := matchLineByValue(userRegexp,line)//始终为假?
 
 // ID始终为第一项。  
 lineIdStr:= line [:strings.IndexByte(line,':')] 
 lineIdInt,err:= strconv.ParseInt(lineIdStr,10,64)
 \  n如果err!= nil {
 panic(err)
} 
 
 //如果匹配,则将ID附加到lineIdArr 
如果匹配{
 lineIdArr = append(lineIdArr,lineIdInt)
} 
}  
} 
 fmt.Println(“返回数组:”,lineIdArr)
 fmt.Println(“使用正则表达式:”,userRegexp)
} 
 
func matchLineByValue(re string,s string)bool {\  n返回regexp.MustCompile(re).MatchString(s)
} 
   
 
 

regexp.MustCompile(re).MatchString(s)不是从用户输入构造正则表达式并将其匹配到整行的正确方法吗?

它匹配的字符串相当长(基本上是整个html文件),会带来问题吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongningwen1146 2018-03-18 18:56
    已采纳

    The call userRegexp, _ := reader.ReadString(' ') returns a string with a trailing newline. Trim the newline:

     userRegexp, err := reader.ReadString('
    ')
     if err != nil {
        // handle error
     }
     userRegexp = userRegexp[:len(userRegexp)-1]
    

    Here's the code with some other improvements (compile regexp once, use scanner Bytes):

    // User input from command line
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter regexp: ")
    userRegexp, err := reader.ReadString('
    ')
    if err != nil {
        log.Fatal(err)
    }
    userRegexp = userRegexp[:len(userRegexp)-1]
    re, err := regexp.Compile(userRegexp)
    if err != nil {
        log.Fatal(err)
    }
    
    // List all .html files in static dir
    files, err := filepath.Glob("static/*.html")
    if err != nil {
        log.Fatal(err)
    }
    
    // Empty array of int64's to be returned with matching results
    var lineIdArr []int64
    
    for _, file := range files {
        htmlFile, _ := os.Open(file)
        fscanner := bufio.NewScanner(htmlFile)
        // Loop over each line
        for fscanner.Scan() {
            line := fscanner.Bytes()
            if !re.Match(line) {
                continue
            }
            lineIdStr := line[:bytes.IndexByte(line, ':')]
            lineIdInt, err := strconv.ParseInt(string(lineIdStr), 10, 64)
            if err != nil {
                log.Fatal(err)
            }
            lineIdArr = append(lineIdArr, lineIdInt)
        }
    }
    fmt.Println("Return array: ", lineIdArr)
    fmt.Println("Using regular expression: ", userRegexp)
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题