英文:
how to get a line from string with a word in golang
问题
我需要从一个多行字符串中获取一行,在golang中有一个常见的单词,比如如果单词是enabled,那么我需要从多行字符串中获取一行once enable then we will continue.。字符串是Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue.。我正在尝试使用以下golang代码:
package main
import (
"fmt"
"regexp"
)
func main() {
s := "Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue."
val := "enabled"
re := regexp.MustCompile(`[^.]*` + val + `[^.]*[\.| ]`)
fmt.Println(re.FindAllString(s, -1))
return
}
英文:
I need to get a line from a multiline string in golang which has common word like if the word is enabled then i need the line from multiline string once enable then we will continue.. The string is Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue. .i am trying this golang code
package main
import (
"fmt"
"regexp"
)
func main() {
s := "Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue."
val := "enabled"
re := regexp.MustCompile(`[^.]*(?i)` + val + `[^.]*[\.| ]`)
fmt.Println(re.FindAllString(s, -1))
return
}
答案1
得分: 1
我不太理解你的问题,但是试试这个:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue."
val := "enable"
for _, sentence := range strings.Split(s, ".") {
if strings.Contains(sentence, val) {
fmt.Println(sentence)
}
}
return
}
如果你的字符串是多行字符串,你应该先按\n
进行分割。
英文:
I am not really understand your question but try this:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Their have a problem with the server.I have to continue the task.Hope the server will enable for everyone.Once enable then we will continue."
val := "enable"
for _, sentence := range strings.Split(s, ".") {
if strings.Contains(sentence, val) {
fmt.Println(sentence)
}
}
return
}
If your string is multipleline string, you should split by '\n' first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论