Non greedy pattern match in sed
Eyal Lebedinsky
eyal at eyal.emu.id.au
Tue Aug 13 20:50:19 EST 2002
Michael Still wrote:
>
> Hey,
>
> I would like non greedy regexp matching in sed.
>
> i.e.
>
> echo "/* This is a */ test /* line */" | sed 's/\/\*.*\*\//g'
> ^^
> Non greedy toggle
> here?
>
> Should result in:
>
> " test "
>
> Any hints?
I am not aware of a general solution, but you can explicitly defeat the
greediness like in:
s/\/\*[^*]*\*\///g
so '.' was replaced with '[^*]' which matches one value less. While
not a foolproof solution, it should work most of the time - it fails
if a non-terminating '*' is embedded in the comment (and if you are
trying to remove comments then you still have problems with multiline
ones).
Well, worth at least $.01?
Note that you missed on '/' at the end of the pattern...
--
Eyal Lebedinsky (eyal at eyal.emu.id.au) <http://samba.org/eyal/>
More information about the linux
mailing list