regex 如何从golang中包含单词字符串中获取行

ruarlubt  于 2023-01-14  发布在  Go
关注(0)|答案(1)|浏览(140)

我需要从golang中的多行字符串中获取一行,该字符串具有常见的单词,如如果该单词是enabled,则我需要从多行字符串中获取该行一旦启用,则我们将继续。。字符串是他们的服务器有问题。我必须继续执行任务。希望服务器将为每个人启用。一旦启用,则我们将继续。。我正在尝试此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(`[^.]*(?i)` + val + `[^.]*[\.| ]`)
    fmt.Println(re.FindAllString(s, -1))
    return
}

This is the playground link

k0pti3hp

k0pti3hp1#

我不是很明白你的问题,但试试这个:

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”拆分。

相关问题