Help with regex
I am trying to truncate a string so that it is only 39 characters long. The application is a label printing routine, and the label is only long enough to print 39 characters. I tried this (and many iterations), but it returns the entire string every time. Can anyone see what I'm doing wrong, or maybe suggest a better way? use strict; use warnings; my $txt = 'This is a string that is longer than thirty nine characters used for testing.'; print \nRunning a test of grabbing the 1st 39 characters of a string.\n; print Test string.: $txt\n; $txt =~ s/^(.{1,39})/$1/; print Resulting string: $txt\n; Barry Brevik ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Help with regex
$txt =~ s/^(.{1,39}).*$/$1/; or $txt = substr($txt,0,39); --T -Original Message- From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Barry Brevik Sent: Thursday, June 30, 2011 11:49 AM To: perl-win32-users@listserv.ActiveState.com Subject: Help with regex I am trying to truncate a string so that it is only 39 characters long. The application is a label printing routine, and the label is only long enough to print 39 characters. I tried this (and many iterations), but it returns the entire string every time. Can anyone see what I'm doing wrong, or maybe suggest a better way? use strict; use warnings; my $txt = 'This is a string that is longer than thirty nine characters used for testing.'; print \nRunning a test of grabbing the 1st 39 characters of a string.\n; print Test string.: $txt\n; $txt =~ s/^(.{1,39})/$1/; print Resulting string: $txt\n; Barry Brevik ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Help with regex
Barry, : I am trying to truncate a string so that it is only 39 characters long. : The application is a label printing routine, and the label is only long : enough to print 39 characters. Wrong tool. Look for substr. Joe Joseph Discenza Senior Analyst/Software Developer 1251 N. Eddy Street, Suite 202 South Bend, IN 46617- 1478 Phone: 574.243.6040 Ext. 233 Fax: 574-243-6060 www.carletoninc.com Visit our blog at: carletoncompliance.blogspot.com This email message is intended only for the addressee(s) and contains information that may be confidential and/or copyrighted. If you are not the intended recipient, please notify the sender by reply email and immediately delete this email. Use, disclosure or reproduction of this email by anyone other than the intended recipient(s) is strictly prohibited. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Help with regex
Wow, thank you all for the many replies I received!! Barry Brevik ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Help with regex
I'm doing some work with mail headers that involves converting timestamps to a standard format. The following regex works except for one pesky trailing close parens. Here's a sample of the data that causes problems: ==sample data Date: Fri, 1 Aug 1997 08:10:16 -0700 (PDT)br === This is converted to a MMDD.hhmmss format in place, then the result is fed to this regex: ==code extract # handle MMDD.hhmmss +0530 (IST) and similar while (/\b ( # $1 to $old (\d{8}\.\d{6}) # $2 to datestamp \s+ ([-+]?\d\d\d\d)# $3 to $timezone ( \s+ [(]? # $4 if there is an abbrev, [A-Z]{2,5}# like EST or (EST) [)]? )? # then just get rid of it ) \b/x ) { my ($old, $d1, $z1, ) = ($1, $2, $3, ); if (exists $timeZones{$z1}) { my $z2 = $timeZones{$z1}; # obtain the abbreviation $z1 = $timeZones{$z2}; # then the numeric value for the abbrev my $d2 = date2Epoch($d1) + 3600 * ($tz - $z1); s/\Q$old\E/'_' . epoch2Date($d2) . ' ' . $tzabbrev/e; } else { s/\Q$old\E/_$old/; # just mark it unchanged } } s/_(\d{8}\.\d{6})/$1/g;# clean up markers return $_; The output I'm getting is ==converted sample Date: 19970801.071016 PST)br The continued existence of that closing parens is the problem. It is not being included in $1, which becomes $old. How can I force its inclusion (and why is the regex not behaving greedily?) -- Will Woodhull [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with regex
What are you trying to get the Epoch time from a date? If so you could also use the Date::Calc modules Date_to_Time function. Hope this helps. Trevor Joerges SendMIME Software www.sendmime.com - Original Message - From: Will of Thornhenge [EMAIL PROTECTED] To: perl-win32-users Mailing List [EMAIL PROTECTED] Sent: Friday, August 01, 2003 8:54 PM Subject: Help with regex I'm doing some work with mail headers that involves converting timestamps to a standard format. The following regex works except for one pesky trailing close parens. Here's a sample of the data that causes problems: ==sample data Date: Fri, 1 Aug 1997 08:10:16 -0700 (PDT)br === This is converted to a MMDD.hhmmss format in place, then the result is fed to this regex: ==code extract # handle MMDD.hhmmss +0530 (IST) and similar while (/\b ( # $1 to $old (\d{8}\.\d{6}) # $2 to datestamp \s+ ([-+]?\d\d\d\d)# $3 to $timezone ( \s+ [(]? # $4 if there is an abbrev, [A-Z]{2,5}# like EST or (EST) [)]? )? # then just get rid of it ) \b/x ) { my ($old, $d1, $z1, ) = ($1, $2, $3, ); if (exists $timeZones{$z1}) { my $z2 = $timeZones{$z1}; # obtain the abbreviation $z1 = $timeZones{$z2}; # then the numeric value for the abbrev my $d2 = date2Epoch($d1) + 3600 * ($tz - $z1); s/\Q$old\E/'_' . epoch2Date($d2) . ' ' . $tzabbrev/e; } else { s/\Q$old\E/_$old/; # just mark it unchanged } } s/_(\d{8}\.\d{6})/$1/g;# clean up markers return $_; The output I'm getting is ==converted sample Date: 19970801.071016 PST)br The continued existence of that closing parens is the problem. It is not being included in $1, which becomes $old. How can I force its inclusion (and why is the regex not behaving greedily?) -- Will Woodhull [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with regex
Will of Thornhenge wrote: I'm doing some work with mail headers that involves converting timestamps to a standard format. The following regex works except for one pesky trailing close parens. Here's a sample of the data that causes problems: ==sample data Date: Fri, 1 Aug 1997 08:10:16 -0700 (PDT)br === This is converted to a MMDD.hhmmss format in place, then the result is fed to this regex: ==code extract # handle MMDD.hhmmss +0530 (IST) and similar while (/\b ( # $1 to $old (\d{8}\.\d{6}) # $2 to datestamp \s+ ([-+]?\d\d\d\d)# $3 to $timezone ( \s+ [(]? # $4 if there is an abbrev, [A-Z]{2,5}# like EST or (EST) [)]? )? # then just get rid of it ) \b/x ) { Drop the closing \b (I changed $4 [optional]): /\b ( # $1 to $old (\d{8}\.\d{6}) # $2 to datestamp \s+ ([-+]?\d{4}) # $3 to $timezone (\s+\([A-Z]{2,5}\))? # $4 if an abbrev, like EST or (EST) ) # then just get rid of it /x) { my ($old, $d1, $z1, ) = ($1, $2, $3, ); if (exists $timeZones{$z1}) { my $z2 = $timeZones{$z1}; # obtain the abbreviation $z1 = $timeZones{$z2}; # then the numeric value for the abbrev my $d2 = date2Epoch($d1) + 3600 * ($tz - $z1); s/\Q$old\E/'_' . epoch2Date($d2) . ' ' . $tzabbrev/e; } else { s/\Q$old\E/_$old/; # just mark it unchanged } } s/_(\d{8}\.\d{6})/$1/g;# clean up markers return $_; The output I'm getting is ==converted sample Date: 19970801.071016 PST)br The continued existence of that closing parens is the problem. It is not being included in $1, which becomes $old. How can I force its inclusion (and why is the regex not behaving greedily?) -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /-- o // // Castle of Medieval Myth Magic http://www.todbe.com/ -/-' /___/__/_/_http://dbecoll.tripod.com/ (Free site for Perl/Lakers) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
help with regex
Hi, Below is a test script im working on: ## $input = '[EMAIL PROTECTED]'; $line = '(.*)@abc.com=$[EMAIL PROTECTED]'; ($left,$right) = split '=', $line,2; $input =~ s/$left/$right/; print $input; results - \[EMAIL PROTECTED] - wrong it should be [EMAIL PROTECTED] ## What I want it to do is given an input $input, the routine will convert the email address into [EMAIL PROTECTED] or whatever the case maybe as defined by $line. I am writing an email address 'alias' converter wherein I have a file of alias rules and base on the rules, it will convert an input into the appropriate email address. some of the rules as straigth forward as: [EMAIL PROTECTED][EMAIL PROTECTED] some are regex based: (.*)@abc.com=$[EMAIL PROTECTED] For versatility, I want the script to take the rules from a text file and so I cannot hardcode the regex rules into the perl script itself. can any one help? thanks jaime ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: help with regex
Jaime Teng wrote: Hi, Below is a test script im working on: ## $input = '[EMAIL PROTECTED]'; $line = '(.*)@abc.com=$[EMAIL PROTECTED]'; ($left,$right) = split '=', $line,2; $input =~ s/$left/$right/; print $input; results - \[EMAIL PROTECTED] - wrong it should be [EMAIL PROTECTED] ## What I want it to do is given an input $input, the routine will convert the email address into [EMAIL PROTECTED] or whatever the case maybe as defined by $line. I am writing an email address 'alias' converter wherein I have a file of alias rules and base on the rules, it will convert an input into the appropriate email address. some of the rules as straigth forward as: [EMAIL PROTECTED][EMAIL PROTECTED] some are regex based: (.*)@abc.com=$[EMAIL PROTECTED] For versatility, I want the script to take the rules from a text file and so I cannot hardcode the regex rules into the perl script itself. can any one help? This should work: my $input = '[EMAIL PROTECTED]'; print Before: $input\n; my $line = '(.*)\@abc.com=$1.abc\@xyz.com'; my ($left, $right) = split '=', $line, 2; eval \$input =~ s/$left/$right/; print After : $input\n; -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles http://www.todbe.com/ / ) /-- o // // Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/ -/-' /___/__/_/_http://www.freeyellow.com/members/dbecoll/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users