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.
CodePudding user response:
Thank you Pak Uula for the tip. Also this answer: 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.