A simple regular expression?
Hi,
with the script shown below I extract the four numbers from the string. #!/usr/bin/perl use 5.10.0; use strict; use warnings; # my $str = "760x35+10+20"; my $re = qr/(d+)[x](d+)[+-](d+)[+-](d+)/; my @line = split(/$re/, $str); say scalar(@line), ": ",join("=",@line); for (@line) { say qq/>$_</; } my ($u, $w, $h, $x, $y) = split(/$re/, $str); say qq/u= >$u< w= >$w< h= >$h< x= >$x< y= >$y</; Result: 5: =760=35=10=20 >< >760< >35< >10< >20< u= >< w= >760< h= >35< x= >10< y= >20< I get one undefined value plus the four numbers. What is the reason? Thanks for any help. -- K.D.J. -- To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org Archive: 4F703B92.2010909@t-online.de">http://lists.debian.org/4F703B92.2010909@t-online.de |
A simple regular expression?
Hi Klaus.
> my $re = qr/(d+)[x](d+)[+-](d+)[+-](d+)/; > my @line = split(/$re/, $str); You are using split the wrong way. The regexp shold only be the *delimiters*. Here's my two alternative ways to accomplish the task: # use the match operator with the regexp my @l2 = $str =~ m/$re/; say scalar(@l2), ": ",join("=",@l2); # split the string based on the delimiters my @l3 = split(/[x+-]/, $str); say scalar(@l3), ": ",join("=",@l3); Both will print the string: 4: 760=35=10=20 -- Pelle "D’ä e å, vett ja”, skrek ja, för ja ble rasen, ”å i åa ä e ö, hörer han lite, d’ä e å, å i åa ä e ö" - Gustav Fröding, 1895 -- To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org Archive: CAOURYnAw1WrNXe6Gx+ezf6cGk9S_OQhmZq80PoywJ+__c=_Gs w@mail.gmail.com">http://lists.debian.org/CAOURYnAw1WrNXe6Gx+ezf6cGk9S_OQhmZq80PoywJ+__c=_Gs w@mail.gmail.com |
A simple regular expression?
This is my version
======================================== #!/usr/bin/env perl use warnings; use strict; my $string = "450x35+60+10"; if($string =~ m/(d+)[x-](d+)+(d+)+(d+)/) { ******* my @list = $string =~ m/(d+)[x-](d+)+(d+)+(d+)/; ******* print scalar @list . ":" . " " . join("=",@list) . " "; } ============================================ Il giorno 26 marzo 2012 12:05, Per Carlson <pelle@hemmop.com> ha scritto: Hi Klaus. > my $re = qr/(d+)[x](d+)[+-](d+)[+-](d+)/; > my @line = split(/$re/, $str); You are using split the wrong way. The regexp shold only be the *delimiters*. Here's my two alternative ways to accomplish the task: # use the match operator with the regexp my @l2 = $str =~ m/$re/; say scalar(@l2), ": ",join("=",@l2); # split the string based on the delimiters my @l3 = split(/[x+-]/, $str); say scalar(@l3), ": ",join("=",@l3); Both will print the string: 4: 760=35=10=20 -- Pelle "D’ä e å, vett ja”, skrek ja, för ja ble rasen, ”å i åa ä e ö, hörer han lite, d’ä e å, å i åa ä e ö" - Gustav Fröding, 1895 -- To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org Archive: http://lists.debian.org/CAOURYnAw1WrNXe6Gx+ezf6cGk9S_OQhmZq80PoywJ+__c_Gsw @mail.gmail.com -- esta es mi vida e me la vivo hasta que dios quiera |
| All times are GMT. The time now is 06:28 AM. |
VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.