作为一个个人项目,尝试通过应用它来学习Go(lang),我正在编写一个EMCAScript / JavaScript“编译器”;它(最初)所做的就是允许你包含其他.js文件。
除了功能之外,我正在试图找出regexp
包裹。以下是看起来不像我想要的那样的片段:
// 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 {
// ...
}
我已经偶然发现了Go的正则表达式子集,因此正则表达式不能读作^\s*include\("([^"]+)"\);
。我已经在RegexPal中测试了首选和Go风格的正则表达式,两者都绝对有效。这场比赛似乎永远不会发生;我做错了什么?
对于它的价值,我试图解析的include()
语句看起来像这样:
include("somefile.js");
编辑:对于它的价值,我保留代码here。
答案 0 :(得分: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 ""
$
答案 1 :(得分:1)
尝试在程序开头添加以下行:
println(runtime.Version())
它应该打印weekly.2012-03-13
或接近该日期的东西。