Extract string after a symbol in Perl

17,866

Solution 1

That's because pattern matches in a scalar context are boolean tests. If you want to capture bracket content (capture groups), you need a list context. It's ok if the list is only one element though:

try this:

 my ( $substring ) = $string=~ /(\:.*)\s*$/;

Difference maybe a bit subtle, but basically - we are assigning 'all the hits' from the pattern match to a list... that comprises one element.

Note - that's so you can do:

my @matches = $string =~ m/(.)/g; 

And get multiple 'hits' returned. If you do as above, you will only get the first match - which is irrelevant given your pattern, but you can do:

my ( $key, $value ) = $string =~ m/(\w+)=(\w+)/; 

for example.

Solution 2

I usually use parentheses to extract a part from text and then refer to the result stored in $1 variable.

look at example:

my $text = "day1: string over here";

print $1 if ($text =~ /:\s*(.+)$/);

but similar result may be recieved with this code too:

my $text = "day1: string over here"; 

my ($a) = $text =~ /:\s*(.+)$/;
print $a;

Solution 3

You can achieve desire substring by using split function also:

#!/usr/bin/perl
use warnings;
use strict;

my $string = "day1: string over here";

my (undef, $substring) = split(':\s*', $string);

print $substring, "\n";

Output:

string over here

Or you can get this by using capturing group () in regex:

my $string = "day1: string over here";
$string =~ m/(.*)\:\s+(.*)$/;
my $substring = $2;
print $substring, "\n";
Share:
17,866
smiikr
Author by

smiikr

Updated on August 25, 2022

Comments

  • smiikr
    smiikr over 1 year

    How can I extract string after a symbol in Perl?

    I tried doing some searches but even the code I found didn't work.

    I'm trying to extract the string after a colon. So I want to show everything after the colon.

    Example:

    string = day1: string over here
    
    substring = string over here
    

    So far I have tried:

    $substring = $string=~ /(\:.*)\s*$/;
    

    But it only outputs the number 1 over and over.

  • Sobrique
    Sobrique over 8 years
    I am not clear why you capture two groups in the regex example, but then ignore one of them.
  • serenesat
    serenesat over 8 years
    OP wanted to extract the string after colon, so I ignored first one.
  • Sobrique
    Sobrique over 8 years
    Granted. But you could just omit the leading (.*) entirely, and just access $1. Or am I missing something?
  • serenesat
    serenesat over 8 years
    I wanted to show OP where is the first part and how capturing group works, so I didn't omit the leading part.