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 991637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:08:29+00:00 2026-05-16T06:08:29+00:00

In Perl, how can I use one regex grouping to capture more than one

  • 0

In Perl, how can I use one regex grouping to capture more than one occurrence that matches it, into several array elements?

For example, for a string:

var1=100 var2=90 var5=hello var3="a, b, c" var7=test var3=hello

to process this with code:

$string = "var1=100 var2=90 var5=hello var3=\"a, b, c\" var7=test var3=hello";

my @array = $string =~ <regular expression here>

for ( my $i = 0; $i < scalar( @array ); $i++ )
{
  print $i.": ".$array[$i]."\n";
}

I would like to see as output:

0: var1=100
1: var2=90
2: var5=hello
3: var3="a, b, c"
4: var7=test
5: var3=hello

What would I use as a regex?

The commonality between things I want to match here is an assignment string pattern, so something like:

my @array = $string =~ m/(\w+=[\w\"\,\s]+)*/;

Where the * indicates one or more occurrences matching the group.

(I discounted using a split() as some matches contain spaces within themselves (i.e. var3…) and would therefore not give desired results.)

With the above regex, I only get:

0: var1=100 var2

Is it possible in a regex? Or addition code required?

Looked at existing answers already, when searching for “perl regex multiple group” but not enough clues:

  • Dealing with multiple capture groups in multiple records
  • Multiple matches within a regex group?
  • Regex: Repeated capturing groups
  • Regex match and grouping
  • How do I regex match with grouping with unknown number of groups
  • awk extract multiple groups from each line
  • Matching multiple regex groups and removing them
  • Perl: Deleting multiple reccuring lines where a certain criterion is met
  • Regex matching into multiple groups per line?
  • PHP RegEx Grouping Multiple Matches
  • How to find multiple occurrences with regex groups?
  • 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-16T06:08:30+00:00Added an answer on May 16, 2026 at 6:08 am
    my $string = "var1=100 var2=90 var5=hello var3=\"a, b, c\" var7=test var3=hello";
    
    while($string =~ /(?:^|\s+)(\S+)\s*=\s*("[^"]*"|\S*)/g) {
            print "<$1> => <$2>\n";
    }
    

    Prints:

    <var1> => <100>
    <var2> => <90>
    <var5> => <hello>
    <var3> => <"a, b, c">
    <var7> => <test>
    <var3> => <hello>
    

    Explanation:

    Last piece first: the g flag at the end means that you can apply the regex to the string multiple times. The second time it will continue matching where the last match ended in the string.

    Now for the regex: (?:^|\s+) matches either the beginning of the string or a group of one or more spaces. This is needed so when the regex is applied next time, we will skip the spaces between the key/value pairs. The ?: means that the parentheses content won’t be captured as group (we don’t need the spaces, only key and value). \S+ matches the variable name. Then we skip any amount of spaces and an equal sign in between. Finally, ("[^"]*"|\S*)/ matches either two quotes with any amount of characters in between, or any amount of non-space characters for the value. Note that the quote matching is pretty fragile and won’t handle escpaped quotes properly, e.g. "\"quoted\"" would result in "\".

    EDIT:

    Since you really want to get the whole assignment, and not the single keys/values, here’s a one-liner that extracts those:

    my @list = $string =~ /(?:^|\s+)((?:\S+)\s*=\s*(?:"[^"]*"|\S*))/g;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large Perl script that is broken into several Perl libraries. In
I am getting the below perl error. Can't use string () as a symbol
Can someone help me with the correct use of the split function in perl
The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use
Does anybody know a Perl library that can parse XML documents and enables me
I've found a Perl regexp that can check if a string is UTF-8 (the
I'm tweaking a perl script that I use to monitor sites (response times etc.)
I'm wondering if there's an idiomatic one-liner or a standard-distribution package/function that I can
Is there a function i can use in Perl to sanitize input before putting
I use a Perl one-liner to create an SQL statement, but I am not

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.