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

Quick question on Regex

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
Hi all,

I reading the files in a dir and extracting a number out of the file names there. ie, I have a folder with filenames such as file1.htm, file2.htm.....file15.htm.....

I am using this to get the numbers off of the end of the filename:

Code:
sub ReadFolder {
opendir (FOLDER, $folder) or die "No path available.";
@filenames= readdir(FOLDER);
closedir(FOLDER);

foreach $file (@filenames) {
	if ($file =~ /prints(\d).*/i)
         {
         # catch the number
         $num = $1;
         # if new num is largest yet, 
         # replace maxNum with current num
         # do this for all files and you will have
         # largest number in the files.
         if ($num > $maxNum) { $maxNum = $num; } 
         } 

}

Works great, except for 1 thing, it only reads the first number so the max that it sees is 9 Is there any way to chenge this to read everything from the end of file to the .htm or do I have to go about this another way?

Thanks in advance,
Jim
 
easy, in the parentheses of the regex, add a '*' immediately after the 'd'. the setup as it was was looking for only a single '\d' character. add the '*' and it will look for and match as many digits as there are consequtively after the text 'prints'. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
I think it might be more appropriate to use a '+' than a '*'. With the '*', you can match zero digits. The '+' will match one or more.

if ($file =~ /prints(\d+).*/i)

'just my two cents worth.


keep the rudder amid ship and beware the odd typo
 
I think you're right about using +.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
oooo. that one always catches me... i thank you both. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top