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

  • Home
  • SEARCH
  • 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 7007707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:38:31+00:00 2026-05-27T21:38:31+00:00

In the following Perl example, a regular expression is used, i.e., next unless s/^(.*?):\s*//;

  • 0

In the following Perl example, a regular expression is used, i.e., next unless s/^(.*?):\s*//; But, how to understand this regular expression, s/^(.*?):\s*//

while ( <> ) {
next unless s/^(.*?):\s*//;
$HoA{$1} = [ split ];
}
  • 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-27T21:38:32+00:00Added an answer on May 27, 2026 at 9:38 pm

    It captures (and stores as $1) some text up to a :. Then it removes the captured text, the semicolon and any trailing whitespace.

    Beyond the regex: if the regex succeeded in doing its job, then the code uses the captured text as a hash key whose value is an array reference. The elements of that array are the rest of the line split on whitespace.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my %HoA;
    
    while ( <DATA> ) {
      #next unless s/^(.*?):\s*//;
      next unless 
        s/      #s is replace match operation
          ^     #start at the beginning of the line
          (     #begin capture $1
            .*? #capture anything, but not greedy, i.e. stop before :
          )     #end capture $1
          :     #literal colon (must match)
          \s*   #optional whitespace
        //x;    #replace match with nothing, x flag allows formatting and comments
      $HoA{$1} = [ split ];
    }
    
    print Dumper(\%HoA), "\n";
    
    __DATA__
    
    Thingy: Thing1 Thing2
    Stuff: mystuff yourstuff
    other line that doesn't have a colon
    

    gives

    $VAR1 = {
              'Thingy' => [
                            'Thing1',
                            'Thing2'
                          ],
              'Stuff' => [
                           'mystuff',
                           'yourstuff'
                         ]
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some Perl regular expression help. The following snippet of code: use strict;
In Perl, I can do this: push(@{$h->[x]}, y); Can I simplify the following python
I'm looking for a pattern for the following. (I'm working in Perl, but I
Per http://perldoc.perl.org/CGI.html to make meta tags, the following example is given: print start_html(-head=>meta({-http_equiv =>
The following perl sub is used to store arrays of hashes. Each hash to
What does reading from <> do in Perl? For example, what will the following
I'm trying to match a regular expression in Perl. My code looks like the
Consider following simple example: #!perl -w use strict; sub max { my ($a, $b)
I came across the following example. I tried to google but could not find
I have been using Perl for a while but is bothered by one syntax

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.