Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Golang正则表达式在2个分隔符之间提取文本-包括分隔符_Regex_String_Go - Fatal编程技术网

Regex Golang正则表达式在2个分隔符之间提取文本-包括分隔符

Regex Golang正则表达式在2个分隔符之间提取文本-包括分隔符,regex,string,go,Regex,String,Go,正如标题中所述,我在golang有一个程序,其中我有一个带重复模式的字符串。我有这个模式的开始和结束分隔符,我想从字符串中提取它们。以下是伪代码: string := "... This is preceding text PATTERN BEGINS HERE ( pattern can continue for any number of lines... ); this is trailing text that is not part of the pattern" 简言之,我试图从上

正如标题中所述,我在golang有一个程序,其中我有一个带重复模式的字符串。我有这个模式的开始和结束分隔符,我想从字符串中提取它们。以下是伪代码:

string := "... This is preceding text
PATTERN BEGINS HERE (
pattern can continue for any number of lines...
);
this is trailing text that is not part of the pattern"
简言之,我试图从上面的例子中提取以“pattern begins HERE”开头并以“;”结尾的模式的所有匹配项,我需要帮助弄清楚这个正则表达式是什么样子

如果需要任何其他信息或上下文,请让我知道

正则表达式是:

(?s)PATTERN BEGINS HERE.*?\);
其中,
(?s)
是允许
*
匹配多行的标志(请参阅)

请参见

不是正则表达式,而是有效的

func findInString(str, start, end string) ([]byte, error) {
    var match []byte
    index := strings.Index(str, start)

    if index == -1 {
        return match, errors.New("Not found")
    }

    index += len(start)

    for {
        char := str[index]

        if strings.HasPrefix(str[index:index+len(match)], end) {
            break
        }

        match = append(match, char)
        index++
    }

    return match, nil
}
编辑:最好将单个字符作为字节处理并返回字节数组