在含转义引号的子句中搜索 Jira:正则表达式的应用
时间:2024-02-08
浏览:190
作者:佚名
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《在含转义引号的子句中搜索 Jira:正则表达式的应用》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
问题内容
regex 101 显示此正则表达式有效:
/during.*\(\"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\",.*\"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\"\)|during.*\(startofmonth\(\),.*now\(\)\)/gm
但在 go 中使用它时,尝试 findallstring 时似乎不起作用(go 演示)
package main
import (
"fmt"
"regexp"
)
var duringregex *regexp.regexp
func init() {
duringregex = regexp.mustcompile(`/during.*\(\"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\",.*\"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\"\)|during.*\(startofmonth\(\),.*now\(\)\)/gm`)
}
func main() {
jqlduringbeginningofmonthtoendofmonth := "project = swb and status changed from \"in regression\" to (done) during (\"2022-09-01\", \"2022-09-30\")"
jqlduringstartofmonthtonow := "project = swb and status changed from \"in regression\" to (done) during (startofmonth(), now())"
fmt.printf("result: %s", duringregex.findallstring(jqlduringbeginningofmonthtoendofmonth, -1))
fmt.println()
fmt.printf("result: %s", duringregex.findallstring(jqlduringstartofmonthtonow, -1))
}
运行节目:
result: [] result: [] Program exited.
我怀疑此行为与 jql 字符串中的转义引号有关。将 jql 字符串更改为使用单引号也不会影响结果。
正确答案
谢谢 pak uula 的提示。还有这个答案:Find all string matches with Regex golang
解决方案是不在正则表达式上使用边界,并且正如 pak 提到的,正则表达式中的反引号负责引号;所以没有必要逃避那些。解决方案在Go Playground
package main
import (
"fmt"
"regexp"
)
var duringregex *regexp.regexp
func init() {
duringregex = regexp.mustcompile(`during.*\("\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])",.*"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])"\)|during.*\(startofmonth\(\),.*now\(\)\)`)
}
func main() {
jqlduringbeginningofmonthtoendofmonth := "project = swb and status changed from \"in regression\" to (done) during (\"2022-09-01\", \"2022-09-30\")"
jqlduringstartofmonthtonow := "project = swb and status changed from \"in regression\" to (done) during (startofmonth(), now())"
fmt.printf("result: %s", duringregex.findallstring(jqlduringbeginningofmonthtoendofmonth, -1))
fmt.println()
fmt.printf("result: %s", duringregex.findallstring(jqlduringstartofmonthtonow, -1))
}
运行节目:
result: [during ("2022-09-01", "2022-09-30")]
result: [during (startOfMonth(), now())]
Program exited.以上就是《在含转义引号的子句中搜索 Jira:正则表达式的应用》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.29
11.29
11.29
11.29
热门专题
