Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:50:56+00:00 2026-05-31T22:50:56+00:00

I have an array of strings of the form: @source = ( something,something2,third ,something,something3

  • 0

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:

  1. Pure regex and then capture $1

    @result = map { (/[^,]+,([^,]*[^, ]) *(,|$)/ )[0] } @source;
    
  2. 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;
    
  3. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:50:58+00:00Added an answer on May 31, 2026 at 10:50 pm

    Use named capture groups and give names to subpatterns with (DEFINE) to greatly improve readability.

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    
    use 5.10.0;  # for named capture buffer and (?&...)
    
    my $second_trimmed_field_pattern = qr/
      (?&FIRST_FIELD) (?&SEP) (?<f2> (?&SECOND_FIELD))
    
      (?(DEFINE)
        # The separator is a comma preceded by optional whitespace.
        # NOTE: the format simple comma separators, NOT full CSV, so
        # we don't have to worry about processing escapes or quoted
        # fields.
        (?<SEP>  \s* ,)
    
        # A field stops matching as soon as it sees a separator
        # or end-of-string, so it matches in similar fashion to
        # a pattern with a non-greedy quantifier.
        (?<FIELD> (?: (?! (?&SEP) | $) .)+ )
    
        # The first field is anchored at start-of-string.
        (?<FIRST_FIELD>  ^  (?&FIELD))
    
        # The second field looks like any other field. The name
        # captures our intent for its use in the main pattern.
        (?<SECOND_FIELD> (?&FIELD))
      )
    /x;
    

    In action:

    my @source = (
         "something,something2,third"
        ,"something,something3   ,third"
        ,"something,something4"
        ,"something,something 5" # Note the space in the middle of the word
    );
    
    for (@source) {
      if (/$second_trimmed_field_pattern/) {
        print "[$+{f2}]\n";
    
        #print "[$1]\n";  # or do it the old-fashioned way
      }
      else {
        chomp;
        print "no match for [$_]\n";
      }
    }
    

    Output:

    [something2]
    [something3]
    [something4]
    [something 5]

    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.

    sub make_second_trimmed_field_pattern {
      my $sep = qr/
        # The separator is a comma preceded by optional whitespace.
        # NOTE: the format simple comma separators, NOT full CSV, so
        # we don't have to worry about processing escapes or quoted
        # fields.
    
        \s* ,
      /x;
    
      my $field = qr/
        # A field stops matching as soon as it sees a separator
        # or end-of-string, so it matches in similar fashion to
        # a pattern with a non-greedy quantifier.
        (?:
            # the next character to be matched is not the
            # beginning of a separator sequence or
            # end-of-string
            (?! $sep | $ )
    
            # ... so consume it
            .
        )+  # ... as many times as possible
      /x;
    
      qr/ ^ $field $sep ($field) /x;
    }
    

    Use it as in

    my @source = ...;  # same as above
    
    my $second_trimmed_field_pattern = make_second_trimmed_field_pattern;
    for (@source) {
      if (/$second_trimmed_field_pattern/) {
        print "[$1]\n";
      }
      else {
        chomp;
        print "no match for [$_]\n";
      }
    }
    

    Output:

    $ perl5.8.8 prog
    [something2]
    [something3]
    [something4]
    [something 5]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of strings of random letters, and I need to know
I have a 2d character array of the form arr[][] . I need to
I have an array of integers in string form: var arr = new string[]
I have array of strings, String[] data and it's 10 elements has value P
I have an array of strings var controlsToGet = new[] {lblHome,lblContact}; I have List<LanguageControl>
I have an array of strings. How can I convert it to System.Collections.ArrayList?
I have an array of strings that I want to use for button titles
I have an array of strings: @array I want to concatenate all strings beginning
I have an array of strings plus one additional string. I want to use
I have an array of strings(as shown in example). I just wish to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.