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.)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs