Skip to content

Search for paragraphs without specific patterns #2077

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

You must be logged in to vote

ripgrep is a line oriented tool. So it's generally not the right tool for this sort of task. By "line oriented tool," what I mean is that its basic mode of operation is to iterate over lines in a file and print only the ones that match. What you want to do requires something more powerful: the ability to reason about lines more holistically than that. ripgrep does not give you that arbitrary power.

With that said, ripgrep does have a -U/--multiline flag that will let you employ such reasoning in a limited way. To achieve what you want, for 1 and 5 lines, respectively:

$ rg -U '(?-m)(^|\n\n)(?P<paragraph>.+($|\n)){1}($|\n)' -o
input
4:row 1

$ rg -U '(?-m)(^|\n\n)(?P<paragraph>.+($|\n)){5}…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by vbd
Comment options

You must be logged in to vote
4 replies
@BurntSushi
Comment options

@vbd
Comment options

@BurntSushi
Comment options

@vbd
Comment options

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