Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove trailing spaces? 2

Status
Not open for further replies.

rcsen

Programmer
Jan 11, 2001
51
US
Hi,
I have given my code below. I tried to remove the trailing spaces in the message field using regular expression but couldn't succeed. Can you suggest how to go about?
Any help will be appreciated.


$pop = new Mail::pOP3Client( USER=>"User name",
PASSWORD=>"Pass",
Host=>"Host Name");
for ($i = 1; $i <= $pop->Count(); $i++)
{
foreach ( $pop->Head( $i ) )
{
$fromaddress = $1 if /From:\s(.+)/;
$replyto = $1 if /To:\s(.+)/;
$subject = $1 if /Subject:\s(.+)/;
}
$message=$pop->Body($i);
}


Thanks

Sincerely,
RCSEN
 
What regex did you try that didn't work? Would maybe just: [tt]s/^(.*?)\s*$/$1/[/tt] work?

Note: there should be people who just sit around and do regex all day. or maybe we could devote huge resources into building a great AI regex generator. we'd all send it problems and it would just figure out the perfect regex answers. *uhg* i sound like a little kid not wanting to do his homework... :p &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
I always use:

s/\s$//go while(/\s$/);

just because it's more readable, to me anyway.

Stillflame, if you'd like to write and support a REGEX AI (You could call it REGinald?) I'm happy to use it. There you are, teamwork! Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi,
I tried both of your regex. But didn't work.
stillflame's regex gives some numeric value (&quot;1&quot;) and the next produces no change.

I tried with
s/\s$/ /;
tr/\n\r\t\f/ /s;

Could you help me?
Thanks.
RCSEN
 
both of those alter the actual strings involved, but they don't return it. run the regex over your text and the spaces should be removed. then just use the same variable name to access it. the return values of s are varied, depending on what context it's in and what modifiers you have at the end, but in this case it looks as if it returned the number of times it correctly matched (once), which means it did work, it'll just be hard to verify at a glance (hence the name 'WHITEspace'). &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
if you say something like:

s/hello/goodbye/;

that will replace the word hello in the string $_ with the word goodbye.

If you want to replace something in another string you have to say:

$MyString ~= s/hello/goodbye/;
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
typo, mike.
'~=' is supposed to be '=~' . &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thanks stillflame and Mike for spending your time. I'll try this.
_rcsen.
 
Ok -- good, strong, technical point there stillflame... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks stillflame, it works fine. But I couldn't get the expected output in Mike's regex. The spaces at the end of the message are still there.
Thanks for the favour.
-RCSEN.
 
rcsen,

Sounds like a good investment for you would be the Oreilly Perl Cookbook or the Learning Regular expressions. No offense, seriously, I don't know may perl programmers that don't have the perl cookbook, it has recipies to get you about %50 through whatever you want to do. That regular expression you've been searching for is in the first chapter, andw ould've saved you some time!

Just a tip..

Mike Baranski.

As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
The isbn # for the cookbook is 1-56592-243-3 As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Thanks mbaranski. Hope I'll get one soon.
-rcsen
 
Or, try

perldoc perlre

This is the documentation As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I always use:

s/\s*$//

Please note the * as compared to MikeLacey's regex of
s/\s$//.

I hope this helps.
 
THe problem may be that there is a linefeed between the trailing spaces and the end of the line. If that's the case, none of those regular expression would match, would they? Try:
Code:
s/\s*\n$/\n/;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top