How to match a block of lines? #371
-
QuestionI've been wondering whether it could be feasible to implement a block-level or paragraph match mode for ugrep. By block-level I mean a tool which would match on a block of contiguous lines defined by regex patterns matching one a start line and another an end line of the block. AnwerThis works out-of-the-box with ugrep with lazy quantifiers and Given a $ ug 'BEGIN.*\n(.*\n)*?.*END' FILE Place the lazy pattern To match a $ ug 'BEGIN(.*\n)*?.*END' FILE For example, to block-match C++ $ ug '/\*(.*\n)*?.*\*+\/' file.cpp To capture the text between two matching lines use option $ ug -P '/\*((.*\n)*?.*)\*+\/' --format="%1%~" file.cpp where To invert the match and output only the blocks that don't match a specific pattern but do match the BEGIN and END criteria? Note that option To include begin and end lines, it will be necessary to use three patterns:
$ ug -e '\A(.*\n)*?.*BEGIN' -e 'END.*\n(.*\n)*?.*BEGIN' -e 'END(.*\n)*\Z' FILE where the third pattern doesn't need to use a lazy quantifier since it matches greedily to the end of the file. Because of this lazy/non-lazy "conflict" at a zero-width anchor $ ug --e '\A(.*\n)*?.*BEGIN' -e 'END.*\n(.*\n)*?(.*BEGIN|\Z)' FILE All of this assumes that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Note: see answer in the question post. |
Beta Was this translation helpful? Give feedback.
Note: see answer in the question post.