How can I remove a substring from a string using Perl? For example, $URL contains http://xyz.com/Main#abcd.aspx
And I want to check and strip out ‘Main#‘ from $URL Can anyone help me out?
Well first I need to check that whether the string Main# exist or not.
If it exists, then strip it; otherwise nothing needs to be done. So only an if statement.
.
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.
This will remove the literal string ‘#Main’ from anywhere in the url if it exists. This could also just be written as
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–