doutun1875 2017-11-09 20:31
浏览 140
已采纳

正则表达式表达式否定设置不起作用golang

I have a regular expression that I'v verified in some online regex parsers

https://regexr.com/3h5h8

^(.*\.(?!(htm|html|class|js)$))?[^.]

How ever implementing this in golang doesn't match the same way the online regex parser does

package main

import (
    "fmt"
    "regexp"
    "strconv"
)

type ParsedConfigFile struct {
    prefix      string
    instanceNum int
    extension   string
}

// tries to guess config type and instance id from files
func parseFiles(files []string) {
    re := regexp.MustCompile(`(type1|type2)_(\d+)\.(csv|ini)`)
    var matchedFiles []ParsedConfigFile

    for _, file := range files {
        match := re.FindStringSubmatch(file)

        // we have 3 groups we try to capture in the regex + 1 for the full match
        EXPECTED_MATCH_COUNT := 4

        if len(match) == EXPECTED_MATCH_COUNT {
            fmt.Printf("trying: %v
", file)
            instanceNum, strConvErr := strconv.Atoi(match[2])

            if strConvErr == nil {
                matchedFiles = append(matchedFiles, ParsedConfigFile{
                    prefix:      match[1],
                    instanceNum: instanceNum,
                    extension:   match[3],
                })
            }
        }

    }
}

func main() {
    files := []string{
        "type1_12.ini",          // match
        "type1_121111.ini",      // match
        "type2_1233.csv",        // match
        "type2_32.csv",          // match
        "type1_.ini",            // don't match
        "type2_32.csv.20141027", // don't match
        "type1_12.",             // don't match
        "_12.ini.",              // don't match
        "_12.ini.11",            // don't match
        "type1_81.ini.20141028", //dont match
        "XNGS.csv",              // don't match
    }

    parseFiles(files)
}

Removing the negated set yields some results but I'm unsure what I have to do mimic the behavior in other regex parser or ignore matches at the end of the filenames

playground link https://play.golang.org/p/6HxutLjnLd

  • 写回答

1条回答 默认 最新

  • drgm51600 2017-11-09 20:40
    关注

    Go's stdlib regexp engine is RE2 which does not support lookaround (e.g. the ?! negative lookahead operator). You can find the complete set of supported Regular Expression syntax in the documentation: https://golang.org/pkg/regexp/syntax/

    If all you need is to ensure that the string ends in a three-character file extension, then you can simplify your expression down to just \.\w{3}$ - a literal period, followed by three characters, followed by the end of the string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?