On 4/27/05, Peter Rabbitson <[EMAIL PROTECTED]> wrote:
> Hello everyone,
> This is the first time I was able to get a complex regex actually working as
> I expect, but I wanted someone to comment on it, in case I am overlooking
> something very major.
> I am trying to throw away a certain string out of random text. The
> occurences might be as follows:
> 
> Free UPS Ground Shipping <rest of string>
> <some of string> free ground shipping !! <rest of string>
> <some of string> free UPS ground shipping!!!
> 
> and all variations of the above. Here is what I did:
> 
> description =~ s/       #take away free ground shipping text
> 
> (?:             #non-capturing block for | inclusion
>    (^)          #start of string
>       |         #or
>    (?<=\S)      #lookbehind non-space character
> )
> 
> \s*             #maybe some spaces
> free            #word 'free'
> \s+             #at least one space
> (?:ups\s+)?     #non-capturing 'ups' with at least one trailing space
> ground          #'ground'
> \s+             #spaces
> shipping        #'shipping'
> \s*             #maybe some spaces
> !*              #maybe some exclamation marks
> \s*             #maybe some more spaces
> 
> (?:             #non-capturing for | inclusion
>    ($)          #end of string
>       |         #or
>    (?=\S\s?)    #lookahead non-space character maybe followed by a space (I 
> want to keep the space if I am cutting from inside a string)
> )
> 
> //ixg;             #replace with nothing
> 
> Seems to be working, but I am afraid it will bite me later. Appreciate any
> comments. The reason I placed all the (?: ) is to speed it up at least a
> bit, I remember reading somewhere that it matters.
> 
> Thanks
> 
> Peter
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 

Peter,

Don't make things so complicated.  You want to find some words and
replace them with nothing.  You don't care where in the string the
pattern appears.  Therefore, you don't have to predict where in the
string it might possibly appear.  As long as what you're looking for
is contiguous, it doesn't matter where it occurs in relation to what
you *aren't* looking for.  You don't have to write a regex for the
enitre line; that's what makes regex so powerful: it does the looking
for you.  Also, ! is a negation.  It may work bare here, but get in
the habit of useing it escaped or in a class.

    $description =~ s/free ups ground shipping[!]*//i;  # or /ig if needed

should work just fine.  Write your patterns to find what is is you're
looking for, not to find what it is you're *not* lokking for.

HTH,

--jay

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to