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