dongwen7813
2018-02-27 09:36
浏览 302
已采纳

匹配单斜杠时出现golang regex错误

I write a pattern and it works in Python, but when I run in Go 1.9.2, it panics:

panic: runtime error: invalid memory address or nil pointer dereference

The code as follows:

package main

import (
    "regexp"
    "fmt"
)



func ReHaveSlash(s string) bool {
    reSlash, _ := regexp.Compile(`^\/(?!\/)(.*?)`)
    a := reSlash.MatchString(s)

    return a

}

func ReHaveSlashdouble(s string) bool {
    reSlash, _ := regexp.Compile(`^\/\/(.*?)`)
    a := reSlash.MatchString(s)

    return a

}

func main() {
    test_url := "/xmars-assets.qiniu.com/archives/1369"
    fmt.Println(ReHaveSlashdouble(test_url))
    fmt.Println(ReHaveSlash(test_url))

}

And the result in Console is as following

false
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1095e56]

goroutine 1 [running]:
regexp.(*Regexp).get(0x0, 0x0)
    /usr/local/Cellar/go/1.9.2/libexec/src/regexp/regexp.go:211 +0x26
regexp.(*Regexp).doExecute(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10ee1f5, 0x25, 0x0, 0x0, ...)
    /usr/local/Cellar/go/1.9.2/libexec/src/regexp/exec.go:420 +0x40
regexp.(*Regexp).doMatch(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10ee1f5, 0x25, 0xc42000a0c0)
    /usr/local/Cellar/go/1.9.2/libexec/src/regexp/exec.go:412 +0xc8
regexp.(*Regexp).MatchString(0x0, 0x10ee1f5, 0x25, 0x115f400)
    /usr/local/Cellar/go/1.9.2/libexec/src/regexp/regexp.go:435 +0x6c
main.ReHaveSlash(0x10ee1f5, 0x25, 0x1)
    /Users/l2017006/Documents/work/check-link/test_re.go:12 +0x58
main.main()
    /Users/l2017006/Documents/work/check-link/test_re.go:29 +0xa5

图片转代码服务由CSDN问答提供 功能建议

我编写了一个模式,它可以在Python中工作,但是当我在Go 1.9.2中运行时,它会出现恐慌:< / p>

 紧急:运行时错误:无效的内存地址或nil指针取消引用
   
 
 

代码如下:

 包main 
 
import(
“ regexp” 
“ fmt” 
)
 
 
 
func ReHaveSlash(s string)bool {
 reSlash,  _:= regexp.Compile(`^ \ /(?!\ /)(。*?)`)
a:= reSlash.MatchString(s)
 
返回a 
 
} 
 
func ReHaveSlashdouble  (s字符串)bool {
 reSlash,_:= regexp.Compile(`^ \ / \ /(。*?)`)
a:= reSlash.MatchString(s)
 
返回a 
 
  } 
 
func main(){
 test_url:=“ /xmars-assets.qiniu.com/archives/1369"
 fmt.Println(ReHaveSlashdouble(test_url))
 fmt.Println(ReHaveSlash(test_url))  
 
} 
   
 
 

控制台中的结果如下

  false 
panic:运行时错误 :无效的内存地址或nil指针取消引用
 [信号SIGSEGV:分段违规代码= 0x1  addr = 0x0 pc = 0x1095e56] 
 
goroutine 1 [正在运行]:
regexp。(* Regexp).get(0x0,0x0)
 /usr/local/Cellar/go/1.9.2/libexec/src/regexp  /regexp.go:211 + 0x26 
regexp。(* Regexp).doExecute(0x0,0x0,0x0,0x0,0x0,0x0,0x10ee1f5,0x25,0x0,0x0,...)
 / usr / local / Cellar  /go/1.9.2/libexec/src/regexp/exec.go:420 + 0x40 
regexp。(* Regexp).doMatch(0x0、0x0、0x0、0x0、0x0、0x0、0x10ee1f5、0x25、0xc42000a0c0)
  /usr/local/Cellar/go/1.9.2/libexec/src/regexp/exec.go:412 + 0xc8 
regexp。(* Regexp).MatchString(0x0,0x10ee1f5,0x25,0x115f400)
 / usr / local  /Cellar/go/1.9.2/libexec/src/regexp/regexp.go:435 + 0x6c 
main.ReHaveSlash(0x10ee1f5、0x25、0x1)
 / Users / l2017006 / Documents / work / check-link / test_re。  go:12 + 0x58 
main.main()
 /Users/l2017006/Documents/work/check-link/test_re.go:29 + 0xa5 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyi1909 2018-02-27 09:52
    已采纳

    Go regexp does not support lookarounds. You may use a negated character class with an alternation group here to fix the issue:

    package main
    
    import (
        "regexp"
        "fmt"
    )
    
    func ReHaveSlash(s string) bool {
        var reSlash = regexp.MustCompile(`^/([^/].*|$)`)
        return reSlash.MatchString(s)
    }
    
    func ReHaveSlashdouble(s string) bool {
        var reSlash = regexp.MustCompile(`^//([^/].*|$)`)
        return reSlash.MatchString(s)
    }
    
    func main() {
        test_url := "/xmars-assets.qiniu.com/archives/1369"
        fmt.Println(ReHaveSlashdouble(test_url))
        fmt.Println(ReHaveSlash(test_url))
    }
    

    See the Go lang demo

    The ^/([^/].*|$) pattern matches a / at the start of the string, and then matches a char other than / followed with any 0+ chars or end of string. The ^//([^/].*|$) matches // followed with any 0+ chars or end of string

    If you want to make sure you only match strings on the same line, replace [^/] with [^/ ] since [^/] also matches line breaks.

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题