在Go语言中匹配段落

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

Matching paragraphs in Go

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个Go语言的初学者。我正在尝试使用regexp来匹配段落:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := `first paragraph

second paragraph

third paragraph`

	r, _ := regexp.Compile(`(.+)(\n\n)`)

	fmt.Println(r.FindAllString(str, -1))
}

输出结果如下:

[first paragraph

 second paragraph

]

我认为它也匹配了空行。我只想匹配段落(first paragraphsecond paragraph)。

如何修改我的代码以实现这一目标?

英文:

I'm a Go beginner. I'm trying to match paragraphs with regexp:

package main

import (
	"fmt"
	"regexp"
)
    
func main() {
	str := `first paragraph

second paragraph

third paragraph`

	r, _ := regexp.Compile(`(.+)(\n\n)`)

	fmt.Println(r.FindAllString(str, -1))
}

The output is this:

[first paragraph

 second paragraph

]

I think it's matching the empty lines also. I only want to match the paragraphs (first paragraph, second paragraph).

How to modify my code to accomplish that?

答案1

得分: 1

你可以尝试使用符合re2规范的正则表达式(?s).*?(\n\n|$)(参见playground示例):

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := `first paragraph
second line of first paragraph

second paragraph

third paragraph
second line of third paragraph`

	r, _ := regexp.Compile(`(?s).*?(\n\n|$)`)

	res := r.FindAllString(str, -1)
	fmt.Printf("%+v %d", res, len(res))
}

这将输出:

[first paragraph
second line of first paragraph

 second paragraph

 third paragraph
second line of third paragraph] 3
英文:

You can try, using a re2-compliant regexp, (?s).*?(\n\n|$) (see playground example):

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := `first paragraph
second line of first paragraph

second paragraph

third paragraph
second line of third paragraph`

	r, _ := regexp.Compile(`(?s).*?(\n\n|$)`)

	res := r.FindAllString(str, -1)
	fmt.Printf("%+v %d", res, len(res))
}

That would output:

[first paragraph
second line of first paragraph

 second paragraph

 third paragraph
second line of third paragraph] 3

huangapple
  • 本文由 发表于 2015年3月3日 16:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/28827207.html
匿名

发表评论

匿名网友

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

确定