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

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

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:

  1. // Note: "lines" is an array of strings.
  2. var includeRegex, _ = regexp.Compile("^[ \t]*include[(]{1}\"([^\"]+)\"[)]{1};")
  3. for _, line := range lines {
  4. var isInclude = includeRegex.Match([]byte(line))
  5. if isInclude {
  6. var includeFile = includeRegex.FindString(line)
  7. fmt.Println("INCLUDE", includeFile)
  8. } else {
  9. // ...
  10. }

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:

  1. include("somefile.js");

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

答案1

得分: 1

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. )
  8. func main() {
  9. includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+");`)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. for _, line := range strings.Split(`
  14. foo
  15. include "abc.def"
  16. include("file.js");
  17. include "me\"to\"";
  18. include("please\"!\"");
  19. nothing here
  20. `, "\n") {
  21. if includeRegex.Match([]byte(line)) {
  22. includeFile := includeRegex.FindString(line)
  23. fmt.Println("INCLUDE", includeFile)
  24. } else {
  25. fmt.Printf("no match for \"%s\"\n", line)
  26. }
  27. }
  28. }

输出:

  1. $ go build && ./tmp
  2. no match for ""
  3. no match for "foo"
  4. no match for "include "abc.def""
  5. INCLUDE include("file.js");
  6. no match for " include "me\"to\"""
  7. INCLUDE include("please\"!\"");
  8. no match for " nothing here "
  9. no match for ""
  10. $
英文:

This seems to work with the latest weekly

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. )
  8. func main() {
  9. includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+"\);`)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. for _, line := range strings.Split(`
  14. foo
  15. include "abc.def"
  16. include("file.js");
  17. include "me\"to\""
  18. include("please\"!\"");
  19. nothing here
  20. `, "\n") {
  21. if includeRegex.Match([]byte(line)) {
  22. includeFile := includeRegex.FindString(line)
  23. fmt.Println("INCLUDE", includeFile)
  24. } else {
  25. fmt.Printf("no match for \"%s\"\n", line)
  26. }
  27. }
  28. }

Output:

  1. $ go build && ./tmp
  2. no match for ""
  3. no match for "foo"
  4. no match for "include "abc.def""
  5. INCLUDE include("file.js");
  6. no match for " include "me\"to\"""
  7. INCLUDE include("please\"!\"");
  8. no match for " nothing here "
  9. no match for ""
  10. $

答案2

得分: 1

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

  1. println(runtime.Version())

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

英文:

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

  1. 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:

确定