Need help with caret anchor at start of line 
Author Message
 Need help with caret anchor at start of line

I have the following text in a file (simulating a message header):




X-Mozilla-Status: 8001

I want to convert the text to:





X-Mozilla-Status: 8001

I use the following while loop, which does what I want:

while (<>) {
   s/[, ]*$//g ;                                # remove commas or spaces at EOL
   s/^[ \t]+(.+\n)/To: $1/i ;           # convert starting space into To:


   s/(?:To:|CC:)\s*//ig ;      
   next if (m/(?:<job|<siang)/) ;
   print OUT $_ ;              

Quote:
}

But I do not understand why the code is not working when I use a caret
as "beginning of the line" anchor in the last two lines:

while (<>) {
   s/[, ]*$//g ;                                # remove commas or spaces at EOL
   s/^[ \t]+(.+\n)/To: $1/i ;           # convert starting space into To:


   s/^(?:To:|CC:)\s*//ig ;              # Remove TO: and cc: at start of line
   next if (m/^(?:<job|<siang)/) ;        
   print OUT $_ ;              

Quote:
}

Can anyone please explain me the reason?
----
Fred


Thu, 02 Jun 2005 04:32:43 GMT  
 Need help with caret anchor at start of line

Quote:

> But I do not understand why the code is not working when I use a caret
> as "beginning of the line" anchor in the last two lines:


                                ^^
                                ^^

Now you have more than one line in $_:

   I'm having trouble matching over more than one line.  What's wrong?

Quote:
>    s/^(?:To:|CC:)\s*//ig ;         # Remove TO: and cc: at start of line

So you need another modifier:

    s/^(?:To:|CC:)\s*//igm;
                         ^
                         ^

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 02 Jun 2005 15:34:01 GMT  
 Need help with caret anchor at start of line

Quote:


(snip)
> So you need another modifier:

>     s/^(?:To:|CC:)\s*//igm;
>                          ^
> --
>     Tad McClellan                          SGML consulting

Oh - you opened my eyes ...
Thank you Tad,

---
Fred



Thu, 02 Jun 2005 16:53:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help substituting text except when in an HTML anchor

2. Anchor matches at start/end of string?

3. Need to output a file starting at a specified line

4. need help getting started with Perl....

5. Need Help getting started with Perl

6. Newbie module developer needs help getting started

7. Need Help To Get Started

8. need help: starting several programs by perl

9. Pre-newbie needs help getting started

10. need help getting started!

11. Need starting help

12. Need help - just starting perl

 

 
Powered by phpBB® Forum Software