.
use strict;
use warnings;
use URI::Split qw( uri_split uri_join );
my $str = "http://xyz.com/Main#abc.aspx"
my ($scheme, $auth, $path, $query, $frag) = uri_split( $str );
That will give you the URI as a series of tokens, but beyond that, the specifics of what you want to do are a bit unclear.
- Are you trying to extract the Path so you can use it?
- Are you trying to recompose the URI without a path?
- Are you trying to extract only a specific node in the path?
- Are you trying to recompose the URI without a specific node in the path
- Are you trying to filter out only the literal string 'Main' , not anything else?
Well first i need to check that
whether the string #Main exist or not,
if it exist then strip it otherwise
nothing to be done, so only an if
statement
if( $str =~ /#Main/ ){
$str =~ s/#Main//g;
}
This will remove the literal string '#Main' from anywhere in the url if it exists. This could also just be written as
$str =~ s/#Main//g;
Because if it doesn't exist, no replacements will be done.
Notable Complications
If you are trying to retrieve a URI from a web-client, as in, it is a request string, you'll likely find the #.*
part, also known as the document fragment, is already removed from the URI when you get it. This is how in my experience web-clients behave.
I'm pretty sure there's an RFC somewhere specifying this to behave like this, but lazyness--