登录
首页 >  Golang >  Go问答

在含转义引号的子句中搜索 Jira:正则表达式的应用

来源:stackoverflow

时间:2024-02-08 14:57:24 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学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>