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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Regex 1

Status
Not open for further replies.

Extension

Programmer
Joined
Nov 3, 2004
Messages
311
Location
CA

Hi,

I have some strings in the following format "05.dat". The filename is always two digits + the extension (.dat)

Now I simply want to keep the two digits before the extension.

Would it be better and more efficient to use a Regex or to use the split function ?

Thanks for your help.
 
Regex is the way to go:
[tt]
($number)=$filename=~/(\d+)\./;
[/tt]
 
Or
Code:
$filename =~ /\.dat$//;
to remove the .dat from the end
 
Missing an "s" there Brian:
Code:
$filename =~ s/\.dat$//;
 
Thanks to everyone. I guess I will stick to a Regex.
 
While the other regex's suggested will work, I would encourage you to error check by more explicitly specifying what you are looking for. The need for this of course depends on what you're doing, but the others will match all sorts of alternate cases.

Code:
($number) = $filename =~ /^(\d\d)\.dat$/;
if ($number eq '') {
    die "problems found in $filename";
}
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
347
  • Locked
  • Question Question
Replies
4
Views
468
  • Locked
  • Question Question
Replies
1
Views
307
  • Locked
  • Question Question
Replies
5
Views
449

Part and Inventory Search

Sponsor

Back
Top