Regexp question...

Regexp question...

Post by Douglas McNaugh » Sun, 22 Feb 1998 04:00:00




> I'm writing a perl script, and one of the things it does is remove
> all trailing spaces from all lines of a text file.  My initial attempt
> at this failed, and I'm not sure why.  Here is the relevant code:

> while ($line = <FILE>) {
>   $line =~ s/^(.*) *\n/$1\n/;
>   $filestring = $filestring . $line;
> }

What's happening is that the * operator is greedy.  It will always try
to match as much as possible, as long as the overall regex matches.
Thus, the (.*) term matches everything up to the end, including all
trailing spaces.  The following ' *' term can succeed by matching
nothing, so it does, and the substitution becomes a no-op, since the
parenthesized term matches the entire string up to the newline.

-Doug
--
sub g{my$i=index$t,$_[0];($i%5,int$i/5)}sub h{substr$t,5*$_[1]+$_[0],1}sub n{(
$_[0]+4)%5}$t='encryptabdfghjklmoqsuvwxz';$c='fxmdwbcmagnyubnyquohyhny';while(
$c=~s/(.)(.)//){($w,$x)=g$1;($y,$z)=g$2;$w==$y&&($p.=h($w,n$x).h($y,n$z))or$x==
$z&&($p.=h(n$w,$x).h(n$y,$z))or($p.=h($y,$x).h($w,$z))}$p=~y/x/ /;print$p,"\n";

 
 
 

Regexp question...

Post by Spencer » Mon, 23 Feb 1998 04:00:00


Hi,

I'm writing a perl script, and one of the things it does is remove
all trailing spaces from all lines of a text file.  My initial attempt
at this failed, and I'm not sure why.  Here is the relevant code:

while ($line = <FILE>) {
  $line =~ s/^(.*) *\n/$1\n/;
  $filestring = $filestring . $line;

Quote:}

I recently discovered that I could just use:
        $line =~ s/ *\n/\n/;
which is simpler and works, but I am curious
as to why my initial attempt failed.

Thanks!

--
Spencer Lu


 
 
 

Regexp question...

Post by Brian McCaule » Mon, 23 Feb 1998 04:00:00



> I'm writing a perl script, and one of the things it does is remove
> all trailing spaces from all lines of a text file.  My initial attempt
> at this failed, and I'm not sure why.  Here is the relevant code:

> while ($line = <FILE>) {
>   $line =~ s/^(.*) *\n/$1\n/;
>   $filestring = $filestring . $line;
> }

The "*" qualifier in regexp matches as many as possible of something.
Where there is abiguity the first one wins.  Your pattern matches as
many as possible of any character followed by as many spaces as
possible.  But spaces are also any character so the first part maches
the log and second part of the pattern always matches the null string.

I've seen some extened versions of regex that have a qualifer that
matches as few as possible of something.  I don't think perl has this
but I may be wrong.

--

 .  _\\__[oo       from       | Phones: +44 121 471 3789 (home)

.  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
 # ll  l\\  ~~~~ ~   ~ ~    ~ | http://wcl-l.bham.ac.uk/~bam/

 
 
 

Regexp question...

Post by Kuntal M. Daftar » Tue, 24 Feb 1998 04:00:00



Quote:> I'm writing a perl script, and one of the things it does is remove
> all trailing spaces from all lines of a text file.  My initial attempt
> at this failed, and I'm not sure why.  Here is the relevant code:

> while ($line = <FILE>) {
>   $line =~ s/^(.*) *\n/$1\n/;
>   $filestring = $filestring . $line;
> }

when simply used, * does greedy match which means that it tries to match as
much as it can.

when u say .*, the * tries to match as much of "anything but newline" and that
includes the trailing spaces. to find out more on greedy match and stingy
match, read up Tom's "what makes perl regexps dynamite" on CPAN archives.

Kuntal Daftary
1.408.527.9789

 
 
 

Regexp question...

Post by Al A » Wed, 25 Feb 1998 04:00:00


                sed "s/  *$//" infile > outfile

--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator                                      sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

 
 
 

1. Regexp Question

depending on what you're trying to do, you could use sed thusly:

sed -e /^SUMMARY/d old.file >new.file

...dave
------------------------------------------------------------------
David W. Capella     | There is an art, or rather a knack
                     | to flying.  The knack lies in learning how

------------------------------------------------------------------

2. Gigabit Ethernet Card with NS Chipset

3. src/regexp question with circmflex "^"

4. "free sco"

5. sed regexp question

6. Create a tar file with a password

7. basic regexp question or not ...

8. does unix have an equivelent to a dynamic linked library?

9. regexp question in trn

10. sed grep regexp question

11. Regexp question

12. regexp question

13. sed and regexp question