Pindo
Pindo

Reputation: 1625

perl remove substring before a character

I've got the following string: get_url.pl?critic_name=A_O_Scott.

I've got the following regex which returns anything before the = but i cant seem to get it to remove before the ?

$string=~ s/\=.*$//;

how can i do this?

Upvotes: 0

Views: 151

Answers (2)

ikegami
ikegami

Reputation: 385789

Use an existing parser rather than writing your own.

use URI             qw( );
use URI::QueryParam qw( );

my $uri = URI->new('get_url.pl?critic_name=A_O_Scott', 'http');

Then, if you want the name of the first parameter,

my ($first_param_name) = $uri->query_param();

That said, most of the time, you'd want a hash of the parameters.

my %params = $uri->query_form();

You said you wanted to remove get_url.pl, but the other answers assumed that's the part you wanted to keep. If you want the part before the query, you can use

use URI qw( );

my $uri = URI->new('get_url.pl?critic_name=A_O_Scott', 'http');
$uri->query(undef);

print("$uri\n");

Upvotes: 5

Flimm
Flimm

Reputation: 150653

if ($string =~ /^(.*)\?(.*)$/) {
    my $first_bit = $1;
    my $second_bit = $2;
}

For the example string a?b, this will set $first_bit to the string a and $second_bit to the string b.

Upvotes: 0

Related Questions