For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Regular Expression not matching correctly









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regular Expression not matching correctly
masked.slacker@gmail.com

2005-08-23, 9:55 pm

I'm trying to remove any lines that contain words (specifically any
sequence of letters greater than two letters) from a given file.

I'm using:

foreach(@lines){
if(($lines[$i] =~ /[A-Za-z]{2,}/)) {
splice @lines, $i, 1;
}
$i++;
}


As I understand it, this should delete any string with 2 or more
sequential letters in it from the array.

However the lines:

qshell= 0.4814201E-01 pshell= 0.11199E+01 tshell= 0.12421E+02
lshell= 0.36366E+00 rshell= 0.98202E-01

and

check irecy= 1 lrecy= 2 iquit= 5

do not get deleted (all other lines that should match do).

So my question is, why is the regular expression not matching these
lines?

LogoX

2005-08-25, 3:55 am

try:

foreach(@lines){
next if /[A-Za-z]{2,}/;
}

masked.slacker@gmail.com wrote:
> I'm trying to remove any lines that contain words (specifically any
> sequence of letters greater than two letters) from a given file.
>
> I'm using:
>
> foreach(@lines){
> if(($lines[$i] =~ /[A-Za-z]{2,}/)) {
> splice @lines, $i, 1;
> }
> $i++;
> }
>
>
> As I understand it, this should delete any string with 2 or more
> sequential letters in it from the array.
>
> However the lines:
>
> qshell= 0.4814201E-01 pshell= 0.11199E+01 tshell= 0.12421E+02
> lshell= 0.36366E+00 rshell= 0.98202E-01
>
> and
>
> check irecy= 1 lrecy= 2 iquit= 5
>
> do not get deleted (all other lines that should match do).
>
> So my question is, why is the regular expression not matching these
> lines?


masked.slacker@gmail.com

2005-08-25, 9:55 pm


LogoX wrote:
> try:
>
> foreach(@lines){
> next if /[A-Za-z]{2,}/;
> }
>


I don't understand what that code is supposed to do.

Its the same regular expression, and lacks the splice that is the whole
point of the loop. Unless my lack of understanding goes farther than I
think.

masked.slacker@gmail.com

2005-08-25, 9:55 pm


LogoX wrote:
> try:
>
> foreach(@lines){
> next if /[A-Za-z]{2,}/;
> }
>


Ok, if gured out what you meant. So I tried:

my $i = 0;
foreach(@lines){
next if /[A-Za-z]{2,}/;
splice @lines, $i, 1;
$i++;
}

Now, the lines that weren't eliminate before are. The ones that were
elminated before are not. So I still have text with more than two
consecutive letters.

masked.slacker@gmail.com

2005-08-25, 9:55 pm


masked.slacker@gmail.com wrote:
> LogoX wrote:
>
> Ok, if gured out what you meant. So I tried:
>
> my $i = 0;
> foreach(@lines){
> next if /[A-Za-z]{2,}/;
> splice @lines, $i, 1;
> $i++;
> }
>
> Now, the lines that weren't eliminate before are. The ones that were
> elminated before are not. So I still have text with more than two
> consecutive letters.


It also deletes dozens of lines of numbers of the form 1.000000E-00,
which should not be deleted. Clearly I am doing something wrong.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com