CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Simple regex problem

 



etheracide
journeyman

Jun 27, 2002, 4:06 PM

Post #1 of 10 (9324 views)
Simple regex problem Can't Post

I am having some difficulty with a rather basic regular expression. I am trying to remove the file extension from a filename to reduce it down to just the name.

Ex. database.dat should become just "database"

turtle.dat should become just "turtle"

I am not sure why I can't get this to work myself...

Thank you for any and all help offered.


fashimpaur
User

Jun 28, 2002, 5:23 AM

Post #2 of 10 (9323 views)
Re: [etheracide] Simple regex problem [In reply to] Can't Post

etheracide,

The proper way to strip off the file name from the extension would probably
be to use the module File::Basename.

If you were going to strip of the the extension with a regex, I would do this:


Code
  

my
$filename = "database.dat";
print (stripExtension($filename), "\n");
$filename = "turtle.dat";
print (stripExtension($filename), "\n");
$filename = "simple.txt";
print (stripExtension($filename), "\n");

sub stripExtension($) {
my $input = shift;
$input =~ /(.*)(?=\.(.+)$)/g ;
my $output = $1;
return $output;
}

Output from script:

database
turtle
simple

Hope that helps.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


NuclearClam
Novice

Aug 6, 2002, 8:56 PM

Post #3 of 10 (9297 views)
Re: [fashimpaur] Simple regex problem [In reply to] Can't Post

sorry to bump an old thread, but what's happening here? (i'm mostly confused about the parameterless shift):
[perl]
my $input = shift;
[/perl]


(This post was edited by NuclearClam on Aug 6, 2002, 9:03 PM)


Jean
User


Aug 7, 2002, 12:41 AM

Post #4 of 10 (9293 views)
Re: [NuclearClam] Simple regex problem [In reply to] Can't Post

1. Parameterless shift uses the default array @_
my $input = shift; is the equivalent of my $input = shift(@_);

2. Function call parameters automatically go into the default array @_
Thus, stripExtension("somename.dat") puts 'somename.dat' as the first element of the @_ array inside the function. Shifting the @_ puts the 'somename.dat' into the $input variable.

Hope that's detailed enough...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Aug 7, 2002, 12:45 AM)


davorg
Thaumaturge / Moderator

Aug 7, 2002, 1:10 AM

Post #5 of 10 (9289 views)
Re: [Jean] Simple regex problem [In reply to] Can't Post


In Reply To
1. Parameterless shift uses the default array @_
my $input = shift; is the equivalent of my $input = shift(@_);

<pedant>
"shift" defaults to using @_ only when called inside a subroutine. Outside of a subroutine it defaults to using @ARGV.
</pedant>

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Jean
User


Aug 7, 2002, 1:31 AM

Post #6 of 10 (9287 views)
Re: [davorg] Simple regex problem [In reply to] Can't Post

Right you are Blush. Moreover, it's a very important note, so don't call youself such names SmileWinkCool


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Aug 7, 2002, 1:32 AM)


NuclearClam
Novice

Aug 7, 2002, 2:07 AM

Post #7 of 10 (9283 views)
Re: [Jean] Simple regex problem [In reply to] Can't Post

i already knew about the default parameters and such, but i'm still learning about a lot of the not-so-apparent things in perl, like the shift thing for example. i'm also still trying to understand some things like this:


Code
 $string = "Yes, we have no bananas, cabbage, or shrubbery."; 
$var = ($string =~ /\s(\w+),/)[0];

why doesn't [1] return "cabbage"? does it only match things once and put it into one element? if so, what's the point? Tongue

one more for the road Tongue

Code
 $blah = q(magic%jellybeans%have%sprouted%in%my%head); 

print bars('in');

sub bars {
print shift; }

why does this return 'in1' instead of just 'in'?


thanks for helping guys Cool



(This post was edited by NuclearClam on Aug 7, 2002, 2:09 AM)


Paul
Enthusiast

Aug 7, 2002, 2:17 AM

Post #8 of 10 (9281 views)
Re: [NuclearClam] Simple regex problem [In reply to] Can't Post

>>
why doesn't [1] return "cabbage"? does it only match things once and put it into one element? if so, what's the point?
<<

....because it is matching a space followed by one or more a-z0-9_ followed by a comma so the first occurance is "bananas".....what's the point?...ask the regex author :)


>>
why does this return 'in1' instead of just 'in'?
<<

Because you are printing the subroutine as well as printing shift.

Try:


Code
$blah = q(magic%jellybeans%have%sprouted%in%my%head);     

print bars('in');

sub bars { shift }


I'm not sure of the relevance of $blah in that example Cool


(This post was edited by RedRum on Aug 7, 2002, 2:19 AM)


NuclearClam
Novice

Aug 7, 2002, 2:34 AM

Post #9 of 10 (9276 views)
Re: [RedRum] Simple regex problem [In reply to] Can't Post

d'oh! i should've known that Tongue

and can you recommend any sites that give the aspiring programmer something to do? like ideas for programs to code or something. the failure i've had with perl in the past was a lack of application. the only times i used it was when i was making programs to do my annoying pre-calc homework. Tongue


Paul
Enthusiast

Aug 7, 2002, 3:32 AM

Post #10 of 10 (9272 views)
Re: [NuclearClam] Simple regex problem [In reply to] Can't Post

You could snoop around here...

http://www.scriptlance.com/

Get paid too :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives