使用正则表达式在包含转义引号的 Jira during 子句中进行搜索。

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

Go regex to search for Jira during clause which contains escaped quotes

问题

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 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\(\)\)/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字符串更改为使用单引号对结果没有影响。

英文:

Regex 101 shows this regex as valid:

/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

but using it in Go, it does not appear to work when attempting FindAllString (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\(\)\)/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))
}

Run shows:

result: []
result: []
Program exited.

I suspect this behaviour has to do with escaped quotes in the JQL strings. Changing the JQL strings to use single quotes did not make a difference in the results, either.

答案1

得分: 0

谢谢Pak Uula的提示。还有这个答案:https://stackoverflow.com/questions/39102902/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.
英文:

Thank you Pak Uula for the tip. Also this answer: https://stackoverflow.com/questions/39102902/find-all-string-matches-with-regex-golang

The solution is to not use boundaries on the regex, and also as Pak mentioned, the back-ticks in the regex take care of the quotes; so no need to escape those. Solution in 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))
}

Run shows:

result: [during ("2022-09-01", "2022-09-30")]
result: [during (startOfMonth(), now())]
Program exited.

huangapple
  • 本文由 发表于 2022年10月29日 08:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74242032.html
匿名

发表评论

匿名网友

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

确定