Golang 正则表达式 – 我做错了什么?

huangapple go评论69阅读模式
英文:

Golang Regular Expressions - what am I doing incorrectly?

问题

作为一个个人项目,我试图通过将其应用于某些东西来学习Go(lang),我正在编写一个EMCAScript/JavaScript“编译器”;它最初只会允许您包含其他.js文件。

除了功能之外,我正在努力弄清楚regexp包。这是一个似乎不按我所希望的方式工作的代码片段:

// 注意:“lines”是一个字符串数组。

var includeRegex,_ = regexp.Compile(“^[ \t]*include[(]{1}"([^"]+)"[)]{1};")
for _,line:= range lines {
var isInclude = includeRegex.Match([]byte(line))
if isInclude {
var includeFile = includeRegex.FindString(line)
fmt.Println(“INCLUDE”,includeFile)
} else {
// ...
}
}

我已经遇到了Go的正则表达式子集,这就是为什么正则表达式不会读取为^\s*include\("([^"]+)"\);的原因。我已经在RegexPal中测试了首选和Go风格的正则表达式,两者都有效。匹配似乎从未发生过;我做错了什么?

值得一提的是,我正在尝试解析的include()语句如下所示:

include(“somefile.js”);

编辑:值得一提的是,我将代码保留在此处。

英文:

As a personal project, trying to learn Go(lang) by applying it to something, I am writing an EMCAScript/JavaScript "compiler"; all it will (initially) do is allow you to include other .js files.

Functionality aside, I am pulling my hair out trying to figure out the regexp package. Here is the snippet that does not seem to be doing what I want it to:

// Note: "lines" is an array of strings.

var includeRegex, _ = regexp.Compile("^[ \t]*include[(]{1}\"([^\"]+)\"[)]{1};")
for _, line := range lines {
   var isInclude = includeRegex.Match([]byte(line))
   if isInclude {
      var includeFile = includeRegex.FindString(line)
      fmt.Println("INCLUDE", includeFile)
   } else {
      // ...
   }

I have already stumbled across Go's subset of regular expressions, hence why the regex does not read as ^\s*include\("([^"]+)"\);. I have already tested both the preferred, and the Go-style regex, in RegexPal, and both definitely work. The match just never seems to occurr; what am I doing wrong?

For what it's worth, the include() statement I am trying to parse looks like so:

include("somefile.js");

EDIT: For what it's worth, I am keeping the code here.

答案1

得分: 1

这似乎适用于最新的每周版本

package main

import (
        "fmt"
        "log"
        "regexp"
        "strings"
)

func main() {
        includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+");`)
        if err != nil {
                log.Fatal(err)
        }

        for _, line := range strings.Split(`
foo
include "abc.def"
include("file.js");
            include "me\"to\"";
            include("please\"!\"");
        nothing here          
`, "\n") {
                if includeRegex.Match([]byte(line)) {
                        includeFile := includeRegex.FindString(line)
                        fmt.Println("INCLUDE", includeFile)
                } else {
                        fmt.Printf("no match for \"%s\"\n", line)
                }
        }
}

输出:

$ go build && ./tmp 
no match for ""
no match for "foo"
no match for "include "abc.def""
INCLUDE include("file.js");
no match for "  	  include "me\"to\"""
INCLUDE   	  include("please\"!\"");
no match for "	nothing here	  "
no match for ""
$
英文:

This seems to work with the latest weekly

package main

import (
        "fmt"
        "log"
        "regexp"
        "strings"
)

func main() {
        includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+"\);`)
        if err != nil {
                log.Fatal(err)
        }

        for _, line := range strings.Split(`
foo
include "abc.def"
include("file.js");
            include "me\"to\""
            include("please\"!\"");
        nothing here          
`, "\n") {
                if includeRegex.Match([]byte(line)) {
                        includeFile := includeRegex.FindString(line)
                        fmt.Println("INCLUDE", includeFile)
                } else {
                        fmt.Printf("no match for \"%s\"\n", line)
                }
        }
}

Output:

$ go build && ./tmp 
no match for ""
no match for "foo"
no match for "include "abc.def""
INCLUDE include("file.js");
no match for "  	  include "me\"to\"""
INCLUDE   	  include("please\"!\"");
no match for "	nothing here	  "
no match for ""
$ 

答案2

得分: 1

尝试将以下行放在程序的开头:

println(runtime.Version())

它应该打印出weekly.2012-03-13或接近该日期的内容。

英文:

Try putting the following line at the start of your program:

println(runtime.Version())

It should print weekly.2012-03-13 or something close to that date.

huangapple
  • 本文由 发表于 2012年3月14日 23:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/9704895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定