Closed ekacoei closed 9 months ago
There are a couple errors in this code:
$
to match end of line or end of string you need to use the Multiline
option (play around with this on https://regex101.com/r/1NSmAJ/1) . I believe Grep always splits on newline and processes each line as a separate string so this doesn't come up. Here's the fixed code:
var puzzleinput string = `1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`
re := regexp2.MustCompile(`^\D*(\d).*(\d)\D*$|((\d))`, regexp2.Multiline)
for m, _ := re.FindStringMatch(puzzleinput); m != nil; m, _ = re.FindNextMatch(m) {
fmt.Printf("Match: %v\n", m.String())
}
Hi there,
I am running on advent-of-code input, it is a leetcode excercise and you are expected to extract a few numbers from a pile of ascii garbage. I expect a PCRE engine to match
$
as either end-of-line or end-of-file. Adding a Dollar sign$
denies me any match.Sample input:
RegEx (extract first and last digit of any line, first and last digit may be the same)
^\D*(\d).*(\d)\D*$|((\d))
Expected output
Sample Golang Programm to illustrate the issue