Golang正则表达式提取两个分隔符之间的文本 – 包括分隔符

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

Golang Regex extract text between 2 delimiters - including delimiters

问题

如标题所述,我有一个用golang编写的程序,其中有一个包含重复模式的字符串。我有一个开始和结束的分隔符来表示这个模式,并且我想从字符串中提取它们。以下是伪代码:

string := "......这是前置文本
PATTERN BEGINS HERE (
模式可以继续多行...
);
这是不属于模式的尾部文本"

简而言之,我想从上面的示例中提取所有以"PATTERN BEGINS HERE"开头,并以");"结尾的模式的所有出现。我需要帮助确定这个正则表达式是什么样子的。

如果需要任何其他信息或上下文,请告诉我。

英文:

As stated in the title I have an program in golang where I have a string with a reoccurring pattern. I have a beginning and end delimiters for this pattern, and I would like to extract them from the string. The following is pseudo code:

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"

In short what I am attempting to do is from the example above is extract all occurrences of of the pattern that begins with "PATTERN BEGINS HERE" and ends with ");" And I need help in figuring out what the regex for this looks like.

Please let me know if any additional info or context is needed.

答案1

得分: 4

正文翻译如下:

正则表达式是:

(?s)PATTERN BEGINS HERE.*?\);

其中 (?s) 是一个标志,让 .* 匹配多行(参见 Go 正则表达式语法)。

查看 演示

英文:

The regex is:

(?s)PATTERN BEGINS HERE.*?\);

where (?s) is a flag to let .* match multiple lines (see Go regex syntax).

See demo

答案2

得分: 3

不是正则表达式,但是有效

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
}

**编辑** 最好将单个字符处理为字节并返回字节数组
英文:

Not regex, but works

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
}

EDIT: Best to handle individual character as bytes and return a byte array

huangapple
  • 本文由 发表于 2017年5月22日 05:49:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/44102135.html
匿名

发表评论

匿名网友

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

确定