I have a string as below:
$var = "ABC/type.xml/BYB?not=1234&at=6789&xyz&LMN";
I want to get the not and at values.. How can work with it??
I have a string as below:
$var = "ABC/type.xml/BYB?not=1234&at=6789&xyz&LMN";
I want to get the not and at values.. How can work with it??
You can, and probably should, use a proper module that can parse this string properly, such as URI
and URI::QueryParam
:
use strict;
use warnings;
use URI;
use URI::QueryParam;
my $str = "ABC/type.xml/BYB?not=1234&at=6789&xyz&LMN";
my $url = URI->new($str);
my $not = $url->query_param('not');
my $at = $url->query_param('at');
print Dumper $not, $at;
Output:
$VAR1 = '1234';
$VAR2 = '6789';
The easiest thing is probably to pull all the values out into a hash in a single shot:
my $var = "ABC/type.xml/BYB?not=1234&at=6789&xyz&LMN";
my %values = $var =~ /(\w+)(?:=(.*?))?(?:&|\Z)/g;
After this, you have
DB<5> x \%values
0 HASH(0x7fa133018f98)
'LMN' => undef
'at' => 6789
'not' => 1234
'xyz' => undef
You could try using regex to extract the matches:
my $var = "ABC/type.xml/BYB?not=1234&at=6789&xyz&LMN";
$var=~/not=(.*?)&/;
my $a=$1;
$var=~/at=(.*?)&/;
my $b=$1;
say $a,",",$b;
Output:
1234,6789