正则表达式表达式否定设置不起作用golang [英] Regex expression negated set not working golang

查看:73
本文介绍了正则表达式表达式否定设置不起作用golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,已经在一些在线正则表达式解析器中进行了验证

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

https://regexr.com/3h5h8

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

在golang中实现此功能与在线正则表达式解析器的方式不同

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\n", 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

游乐场链接 https://play.golang.org/p/6HxutLjnLd

推荐答案

Go的stdlib regexp引擎是RE2,它不支持环视(例如?!负前瞻运算符).您可以在文档中找到完整的受支持的正则表达式语法: https://golang.org/pkg/regexp/syntax/

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/

如果只需要确保字符串以三个字符的文件扩展名结尾,则可以将表达式简化为 \.\ w {3} $ -一个文字时间,然后是三个字符,然后是字符串的结尾.

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.

这篇关于正则表达式表达式否定设置不起作用golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆