[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: 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) . "\n";
}
============================================

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

Reply to: