Deano
Deano

Reputation: 12190

Get the value of a variable from another

Can you assist me in determing correct $string = line to end up with partial_phone containing 4165867111?

sub phoneno {
my ($string) = @_;
$string =~  s/^\+*0*1*//g;
return $string;
}

my $phone = "<sip:+4165867111@something;tag=somethingelse>";

my $partial_phone = phoneno($phone);

Upvotes: 0

Views: 80

Answers (3)

Kenosis
Kenosis

Reputation: 6204

This will capture all digits preceding the @:

use strict;
use warnings;

sub phoneno {
    my ($string) = @_;
    my ($phoneNo) = $string =~ /(\d+)\@/;
    return $phoneNo;
}

my $phone = '<sip:+4165867111@something;tag=somethingelse>';

my $partial_phone = phoneno($phone);

print $partial_phone;

Output:

4165867111

Upvotes: 2

Perleone
Perleone

Reputation: 4038

$string =~ s{
    \A          # beginning of string
    .+          # any characters
    \+          # literal +
    (           # begin capture to $1
        \d{5,}  # at least five digits
    )           # end capture to $`
    \@          # literal @
    .+          # any characters
    \z          # end of string
}{$1}xms;

Upvotes: 3

mob
mob

Reputation: 118605

Your substitution starts with a ^, which means it won't perform substitution unless the rest of your pattern matches the start of your string.

There are lots of ways to do this. How about

my ($partial) = $phone =~ /([2-9]\d+)/;
return $partial;

This returns any string of digits that doesn't begin with a 0 or 1.

Upvotes: 2

Related Questions