I have an array of strings of the form:
@source = (
"something,something2,third"
,"something,something3 ,third"
,"something,something4"
,"something,something 5" # Note the space in the middle of the word
);
I need a regex which will extract the second of the comma separated words, BUT without the trailing spaces, putting those second words in an array.
@expected_result = ("something2","something3","something4","something 5");
What is the most readable way of achieving this?
I have 3 possibilities, neither of which seems optimal readability wise:
-
Pure regex and then capture $1
@result = map { (/[^,]+,([^,]*[^, ]) *(,|$)/ )[0] } @source; -
Split on commas (this is NOT a CSV so no parsing needed), then trim:
@result = map { my @s = split(","), $s[1] =~ s/ *$//; $s[1] } @source; -
Put split and trim into nested
maps@result = map { s/ *$//; $_ } map { (split(","))[1] } @source;
Which one of these is better? Any other even more readable alternative I’m not thinking of?
Use named capture groups and give names to subpatterns with
(DEFINE)to greatly improve readability.In action:
Output:
You can express it similarly to older perls. Below, I confine the pieces to the lexical scope of a sub to show that they all work together as a unit.
Use it as in
Output: