[prog] perl string question

mc mcgonzalez at att.net
Thu Feb 27 19:17:12 EST 2003


On Thu, 2003-02-27 at 17:59, k.clair wrote:
> oh! sorry... i got distracted by regex's and mis-split your string!

Not a problem, I was still going through all the regex stuff line by
line to totally understand it :)

> 
> if you want to have everything after Message-ID be on the new line, you
> can't rely on the colon followed by the space...
> 
> so i think this would work better:
> 
> $string = "^~00002439:0000007179:000016:Message-ID: <3E33C3BD.21CD8BB6 at att.net>"
> $string =~ /^(.*:)(Message-ID.*)$/;
> $first_part = $1;
> $second_part = $2;
> 
> and now it occurs to me that you could actually use substitution as
> well--
> 
> $string =~ s/^(.*:)(Message-ID.*)$/$1\n$2/;
> 
> so the substitution looks a lot like a regular expression, but it has
> three /'s rather than just two:  s///;
> what happens is that perl matches the string against the regular
> expression between the first two /'s, and then it replaces the matched
> portion of the string (which is the entire string in this case) with
> what is between the second and third /.  just like we used $1 and $2
> after the matching in the straight regex, you can use the $1 and $2
> variables in the second part of the substitution function, and you can
> also insert a newline between them.
> this way, now you can just print $string and it will already contain the
> line separation...
> 
> the new string should be:
> ^~00002439:0000007179:000016:\nMessage-ID: <3E33C3BD.21CD8BB6 at att.net>
> 
> ok, i hope that wasn't too much (and also that it was helpful and not
> things you already knew!)
> 
> kristina
> 
Definitely not stuff I knew at all, very helpful as well as great stuff
for me to learn :)  Thanks so much!!!


-- 
mc
I haven't lost my mind,
It is backed up on disk somewhere.
4M




More information about the Programming mailing list