I want to grep the shortest match and the pattern should be something like:
<car ... model=BMW ...>
...
...
...
</car>
... means any character and the input is multiple lines.
I want to grep the shortest match and the pattern should be something like:
<car ... model=BMW ...>
...
...
...
</car>
... means any character and the input is multiple lines.
The short answer is using the next regular expression:
A (little) more complicated answer is:
This will makes possible to match car1 and car2 in the following text
grep
For non-greedy match in
grep
you could use a negated character class. In other words, try to avoid wildcards.For example, to fetch all links to jpeg files from the page content, you'd use:
To deal with multiple line, pipe the input through
xargs
first. For performance, useripgrep
.Actualy the
.*?
only works inperl
. I am not sure what the equivalent grep extended regexp syntax would be. Fortunately you can use perl syntax with grep sogrep -P
would work butgrep -E
which is same asegrep
would not work (it would be greedy).See also: http://blog.vinceliu.com/2008/02/non-greedy-regular-expression-matching.html
My grep that works after trying out stuff in this thread:
Just make sure you append a space to each one of your lines
(Mine was a line by line search to spit out words)
You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier
?
after the quantifier. For example you can change.*
to.*?
.By default
grep
doesn't support non-greedy modifiers, but you can usegrep -P
to use the Perl syntax.