**众所周知,golang的正则表达式是不支持反向引用的,有没有谁搞过这一块?有没有一个比较好的替代方案?**
我表达跟后向引用有关,就是后向预查,况且你这也不是后向引用,你只是在正则匹配后,从缓冲区取,我说的后向引用是下面这种情况,golang没法匹配。
enNodeRx := regexp.MustCompile(`<(p|h[0-9]|ul|ol)[^>]*class=['"]?en['"]?[^>]*>.*</$1>`)
s := `<p class="en">Because these channels use the network, there is alwasy the possibility of network errors leading to timeouts. Andrew Gerrand points out a solution using <a href="http://blog.golang.org/2010/09/go-concurrency-patterns-timing-out-and.html">timeouts</a>: "[Set up a timeout channel.] We can then use a select statement to receive from either ch or timeout. If nothing arrives on ch after one second, the timeout case is selected and the attempt to read from ch is abandoned."</p>`
matched := enNodeRx.Find([]byte(s))
fmt.Println(string(matched))
#6
更多评论
谁说不支持反向引用?还众所周知?
package main
import (
"regexp"
"fmt"
)
func main() {
re := regexp.MustCompile("([0-9]+)年([0-9]+)月([0-9]+)日")
fmt.Println(re.ReplaceAllString("2013年9月30日", "$1-$2-$3"))
}
输出结果:
2013-9-30
注:一般的语言用的是 \1 这种形式反向引用
#1