why does --only-matching only show the shortest match when searching for multiple patterns? #1874
-
What version of ripgrep are you using?ripgrep 12.1.1 How did you install ripgrep?Via the latest nix package What operating system are you using ripgrep on?
Describe your bug.If I ask ripgrep to find two strings, where the first string is a substring of the other, and use --only-matching, only the shorter string is printed. If you pass the longer string first, you get the expected output. See a simple repo here: https://replit.com/@lpetre/ripgrep-issue What are the steps to reproduce the behavior?
What is the actual behavior?
What is the expected behavior?I think ripgrep should show the longer result regardless of the order of the input.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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:
Indeed, given patterns p1 and p2, where p1 comes before p2 in the pattern list and where p1 is a prefix of p2, it follows that p2 will never be reported as a match. Because of this property, if your patterns are simple literals, and if you sort them in descending order by length, then it will report precisely the same results as POSIX grep. It is plausible that ripgrep will gain "proper" support for POSIX leftmost-longest semantics in the future, but it's likely years away from happening. |
Beta Was this translation helpful? Give feedback.
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:
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…