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

Re: 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


Reply to: