Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Add a flag to return the intersection of results if multiple patterns are given #2043

Closed
ctittel opened this issue Oct 26, 2021 · 1 comment
Labels
duplicate An issue that is duplicative of another.

Comments

@ctittel
Copy link

ctittel commented Oct 26, 2021

The flag -e / --regexp allows one to search multiple patterns.

Currently, a search rg -e pattern1 -e pattern2 returns the union of the results that would be returned by the two searches rg -e pattern1 and rg -e pattern2.
The files returned by rg -e pattern1 -e pattern2 are the files that contain at least one of [pattern1, pattern2].

My request is to add a flag (e.g. --conjunction) where files have to contain all the given patterns to be a match.
So the command rg -e pattern1 -e pattern2 --conjunction would return the files that contain both pattern1' and pattern2

Another thing that has to be taken into account here is whether multiline search is performed or not.
If multiline search is performed, the patterns could be allowed to be on different lines, if multiline search is not performed the patterns could be required to all be on the same line.

I currently use this command to iteratively filter the files to get the files where all given patterns are present:

for QUERY in $TERMS; do
   FILES=$(echo "$FILES" | xargs -d $'\n' rg -L -H --multiline --multiline-dotall --files-with-matches $QUERY)
done

Simply constructing a regex like pattern1 .* pattern2 is not an option here, because the patterns should be allowed to appear in any order.
For two patterns you would have to search for pattern1 .* pattern2 and pattern2 .* pattern1.
For three patterns, there would of course be 6 different orders in which the patterns could appear, etc.

So the flag would not strictly be required in the multiline-search case (as I have demonstrated), but it would be much more elegant.
In the single-line search case however (where you want all patterns in the same line, in any order) such a flag would be required, because the only other way would be to construct a very long and complicated regular expression.

@BurntSushi
Copy link
Owner

Dupe of #875

@BurntSushi BurntSushi added the duplicate An issue that is duplicative of another. label Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate An issue that is duplicative of another.
Projects
None yet
Development

No branches or pull requests

2 participants