Click to See Complete Forum and Search --> : Is there a way of simplifying this regex?


bubblenut
12-01-2003, 12:21 PM
Hi is there a way of simplifying this regex?

grep “[^\(_0\)\(_1\)\(_e\)\(_gib\)\(pv\)]_vouchers[\.\ ]”

Thanks Bubble

dchidelf
12-01-2003, 05:55 PM
I'm guessing that you are trying to match

"_vouchers." or "_vouchers "
as long as it is not preceeded by
_0 _1 _e _gib or pv

Your current regex matches

"_vouchers." or "_vouchers "
as long as the preceeding character is not
\ ( ) _ 0 1 e g i b p or v


Off the top of my head I can't think of a good way to do this in one grep statement.
You may need to use:

| egrep -v "(_0|_1|_e|_gib|pv)_vouchers[\.\ ]" | grep "_vouchers[\.\ ]"


This should grab all lines not matching lines with the undesired prefixes to vouchers, then limit those results to lines that still contain vouchers (so you don't get lines that don't say _vouchers at all)

perl provides negative lookback that lets you match as long as the match isn't preceeded by xyz. Perl requires that the lookback string be a static length, which yours is not (because _gib is longer than the rest). You may be able to craft something with it though.

This is what it looks like without _gib

perl -ne '/(?<!_0|_1|_e|pv)_vouchers[\. ]/ && print '