In a message dated 4/20/2006 5:55:08 P.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> Craig Cardimon wrote:
> > I'm looking for patterns where the sign "%" or the word "percent" are
> > preceded by a group of one or more digits and zero or more spaces.
> >
> > if( $line =~ /\%|\bpercent\b/i )
> > {
> >     if( $line =~ /\d+\s*(\%|\bpercent\b/i )
> >     {
> >          # magic happens
> >     }
> > }
> >
> > Am I on the right track here? In the text I'm searching, too many "%"
> > signs are preceded by only "confidential", "***", "[confidential]",
> > "[***]", or other various unhelpful things. I need a better way to
> > screen out the trash and screen in the goodies.
> >
> I'm not sure that the '\b's are doing much for you in this case (it
> depends on your input).  Also you're missing a ")"  after the last \b.
> #!/usr/bin/perl -w
> use strict;
>
> while (<DATA>)
> {
>    chomp;
>
>     if (/(\d+)\s*(\%|percent)/i )
>     {
>          print "found $1 percent\n"; # magic happens
>     }
>     else
>     {
>      print "Line $_ doesn't match\n";
>     }
> }
>
> __END__
> 100 %
> 50 percent
> confidential%
> *** percent
> [confidential]percent
> [***] %
> 76%
>
> Returns:
> found 100 percent
> found 50 percent
> Line confidential% doesn't match
> Line *** percent doesn't match
> Line [confidential]percent doesn't match
> Line [***] % doesn't match
> found 76 percent
>
> This will break if you're expecting other non-digit characters in your
> percentage, (like a decimal point or a negative sign.)
 
it also breaks if something like ``50 percent'' is split across two lines.  
also if you have to deal with some legalism like (shudder) ``fifty (50) percent'' or
``50 (fifty) percent''.   the first case is fairly easy to deal with if you can safely slurp
the entire file; a little more hassle is needed if you must process line-by-line.  
the second case is more problematic.  
 
hth (but it probably doesn't, much) -- bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to