Skip to content

why does --only-matching only show the shortest match when searching for multiple patterns? #1874

Answered by BurntSushi
lpetre asked this question in Q&A
Discussion options

You must be logged in to vote

This result here correct behavior and intended, although your description of it is incorrect. ripgrep does not print the shortest match. It prints the first match. In contrast, POSIX grep will always show the longest match. ripgrep implements leftmost-first or "preference order" matching, which corresponds to how backtracking regex engines report matches. If you flip the order of your patterns, you can see how preference order works:

$ cat /tmp/input.txt
this.is.a.string.too
this.is.a.string
$ rg -f /tmp/input.txt -o /tmp/input.txt
1:this.is.a.string.too
2:this.is.a.string

Indeed, given patterns p1 and p2, where p1 comes before p2 in the pattern list and where p1 is a prefix of p2, it fo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1873 on May 26, 2021 12:50.