tags:

views:

257

answers:

5

Hi. I have a line:

$myline = 'ca,cb,cc,cd,ce';

I need to match ca into $1, cb into $2, etc..

Unfortunately

$myline =~ /(?:(\w+),?)+/;

doesn't work. With pcretest it only matches 'ce' into $1. How to do it right? Do I need to put it into the while loop?

Thanks!

+9  A: 

Isn't it easier to use my @parts = split(/,/, $myline) ?

Dana
That's right! The easiest way is to use split!
Dmytro Leonenko
+9  A: 

Why not use the split function:

@parts = split(/,/,$myline);

split splits a string into a list of strings using the regular expression you supply as a separator.

Dave Webb
+1  A: 

If the number of elements is variable, then you're not going to do it in the way you're aiming for. Loop through the string using the global flag:

while($myline =~ /(\w+)\b/g) {
    # do something with $1
}

I am going to guess that your real data is more complex than 'ca,cb,cc,cd,ce', however if it isn't then the use of regular expressions probably isn't warranted. You'd be better off splitting the string on the delimiting character:

my @things = split ',', $myline;
Quick Joe Smith
You're right. It's much better to use split in my case. Why don't I thought of it?
Dmytro Leonenko
Because it's Perl, and there are so many ways to do it.
Quick Joe Smith
+3  A: 

Although split is a good way to solve your problem, a capturing regex in list context also works well. It's useful to know about both approaches.

my $line = 'ca,cb,cc,cd,ce';
my @words = $line =~ /(\w+)/g;
FM
A notable difference is that split will preserve empty entries, giving `undef` in spots where commas are adjacent. The regex method ignores these places as they don't contain one or more word characters.
Adam Bellaire
+3  A: 

Look into the CSV PM's you can download from cpan, i.e., Text::CSV or Text::CSV_XS

This will get you what you need and also account for any comma seperated values that happen to be quoted.

Using these modules make it easy to split the data out and parse through it...

ex.) my @field = $csv->fields;

Courtland