Gossamer Forum
Home : General : Perl Programming :

Replace digit at the end of a string?

Quote Reply
Replace digit at the end of a string?
Hi. I have a string, like;

Code:
$_ = 'Universities Worldwide Search Argentina2.htm';

$_ =~ s/Universities Worldwide Search //;
$_ =~ s/\.htm|\.html//;
$_ =~ s/\d$//;

The part in red just doesn't seem to be doing what its supposed to. Anyone got any ideas on how I can remove the digit from the end? (its not on the end of all the array values).

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
Hi Andy,

That works for me:

Code:
[14:59][piper@galaxy:~]$ perl -e '
my $string = "Universities Worldwide Search Argentina2.htm";
$string =~ s/Universities Worldwide Search //;
$string =~ s/\.htm|\.html//;
$string =~ s/\d$//;
print "\n$string\n";'

Argentina
[14:59][piper@galaxy:~]$

Do you have new lines or other characters hiding at the end of the string maybe?

~Charlie
Quote Reply
Re: [Chaz] Replace digit at the end of a string? In reply to
Yeah, seem a bit weird. Because the variables don't have any other digits in them, all I did was use a generic replace of all digits.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
Hey Andy, I take it all the values of $_ will contain "Universities Worldwide Search ", right?

Assuming that, if all you want to do is get the name of the country without that string and drop any possible numbers after it and the two possible extensions (.htm and .html), then this expression should do the trick:

my $string = 'Universities Worldwide Search Argentina2.htm';
$string =~ m/Universities Worldwide Search ([^0-9]*)\d*\.html?$/;
print $1;

Basically: match the part of the string following "...Search " which does not contain numbers, followed by zero or more numbers, followed by ".htm" followed by zero or one "l" and start matching at the end of the string.

Let me know if that works for you.

Last edited by:

Crolguvar: Nov 20, 2003, 6:39 AM
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
print $1 if s/^.*\s+([^\d]+).*$//;

-g

s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: [Crolguvar] Replace digit at the end of a string? In reply to
Thanks, but the job I was using this in has already been completed. It was a quick script to go through a list of files, and grab a list of URL's from it. Thanks for taking the time to reply though.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [GClemmons] Replace digit at the end of a string? In reply to
In Reply To:
print $1 if s/^.*\s+([^\d]+).*$//;

-g

Show off :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
Me? Never ;)

-g

s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
FWIW, when you are matching or substituting using $_ then you can omit it from the code, for example:

Code:

if ($_ =~ /abc/) {


...equates to:

Code:

if (/abc/) {


Regarding the actual scenario you faced, performing a substitution actually modifies the variable, however if you only wanted to get the matching text, something like this may have worked....

Code:

my ($match) = $_ =~ /(\w+)\d+\.html?$/;
Quote Reply
Re: [Recall] Replace digit at the end of a string? In reply to
Thanks for the reply. Wouldn't it have been this though?

Code:
my ($match) = $_ =~ /(\w+)\d+(\.html|\.htm)?$/;

The extensions vary between .html and .htm.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Replace digit at the end of a string? In reply to
Notice I have a "?" after the .html ...that means the "l" is optional so it will match .htm or .html