英文:
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 paragraph
,second 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论