http://qs321.pair.com?node_id=986559


in reply to Re: Regex Extraction Help
in thread Regex Extraction Help

invaderzard, just wanted to make clear that this solution by Kenosis is the far quicker and easier version, which I'd, of course use anytime I'd just need a quick split by a field separator on an input.

But: There is one caveat here to keep in mind. Split, of course, does not test the format of the input. So if you wanted the second field of a record that goes like this:

$record = 'A;B;C;D';
then
$second_field = (split ';', $record)[1];

does work. However so it does for inputs like:

A;B #foo;B;ar ;B

All of the above inputs would leave a B in $second_field. Which, you know might be correct in a particular case, but in general, we don't want to just ignore malformed records, so if we, say, iterate over records, then make sure to test and capture using a regexp in an if:

if($record =~ m/^.;(.);.;.$/) { $second_field = $1; }

Now this will only set $second_field if the record matches the four single-character fields delimited by one semicolon format. Even if the input is ';;;;;;;'. ;)

Have fun with regexen. They're cool. ;)

So long,
Flexx