diff --git a/main.go b/main.go index 53ff644..7742649 100644 --- a/main.go +++ b/main.go @@ -705,8 +705,14 @@ func main() { if !(*multiLineFlag) { fmt.Fprintf(out, "Line %d:\n", lineNum) } - for _, idx := range matchIndices { - fmt.Fprintf(out, "%s\n", idx.toString()) + for i, m := range matchIndices { + // Only print a match if: + // a. We are _not_ printing just one match + // OR + // b. We _are_ printing just one match, and this is that match + if !matchNumFlagEnabled || (i+1) == *matchNum { // Match indexes start from 1; loop counter starts from 0 + fmt.Fprintf(out, "%s\n", m.toString()) + } } err := out.Flush() if err != nil {