Ale*_*. b 15 regex string algorithm go
我正在尝试提取内部的任何数据${}.
例如,从该字符串中提取的数据应该是abc.
git commit -m '${abc}'
Run Code Online (Sandbox Code Playgroud)
这是实际的代码:
re := regexp.MustCompile("${*}")
match := re.FindStringSubmatch(command)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,任何想法?
roc*_*987 29
你需要逃避$,{并}在正则表达式.
re := regexp.MustCompile("\\$\\{(.*?)\\}")
match := re.FindStringSubmatch("git commit -m '${abc}'")
fmt.Println(match[1])
Run Code Online (Sandbox Code Playgroud)
在正则表达式中,
$ <-- End of string
{} <-- Contains the range. e.g. a{1,2}
Run Code Online (Sandbox Code Playgroud)
你也可以使用
re := regexp.MustCompile(`\$\{([^}]*)\}`)
Run Code Online (Sandbox Code Playgroud)