I have an array of strings:
@source = ( "something,something2,third" ,"something,something3 ,third" ,"something,something4" ,"something,something 5"
I need a regular expression that will extract the second of the words separated by commas, BUT without trailing spaces, putting these words in an array.
@expected_result = ("something2","something3","something4","something 5");
What is the most readable way to achieve this?
I have 3 possibilities, none of which seem like optimal readability:
Pure regex and then grab $ 1
@result = map { (/[^,]+,([^,]*[^, ]) *(,|$)/ )[0] } @source;
Separate the comma (this is NOT a CSV, so no parsing is required), then crop:
@result = map { my @s = split(","), $s[1] =~ s/ *$//; $s[1] } @source;
Put split and trim in nested map s
@result = map { s/ *$//; $_ } map { (split(","))[1] } @source;
Which one is better? Any other, more readable alternative that I don't think about?
source share