Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a String...

Status
Not open for further replies.

ameldridge

Technical User
Jan 20, 2009
1
GB
Hi, it's getting late and this has been driving me mad :)

I've got the following string and need to strip out the numerical characters from near the end of the URL (so just 75118 in this example), the end of the URL will always end in .aspx

$url = "
I would be most grateful if someone could point me in the right direction, thanks in advance.
 
Hi,

Personally I would use File::Basename to pull out just 75118.aspx and use a regular expression / split to to separate the prefix and suffix.

Code:
use File::Basename;
my $file = basename($url);
#Using split...
my($prefix,$suffix) = split(/\./,$file);
#OR using a regular expression...
$file =~ /^(\d+)\.(aspx)$/
my($prefix,$suffix) = ($1,$2);

If you don't want to use File::Basename then you could use split or a regular expression again (probably easiest using split).

Code:
my @split = (/\//,$url);
my $file = $split[-1];

Hope this helps,

Chris
 
A cleaner 1 liner to split $url by / and . at the same time:

Code:
###$parts[-2] is 75118
my @parts = split(/[\/.]/,$url);

Chris
 
Code:
$url = "[URL unfurl="true"]http://www.pandora-jewelry.com/UK/Jewelry/Charms/Clips/75118.aspx";[/URL]
$url =~ s{/\d+\.aspx$}{};
print $url;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
For clarity in Kevin's post

the $ in .aspx$ is anchoring the regex to the end of the string
^ is used to anchor to the start of the string

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top