List Info

Thread: Another regular expression question




Another regular expression question
user name
2006-07-07 15:43:44

Hi,

I have file name, such as:

My $file = "c:\views\12352_Utilities\na_gcs_12352fmcdealer\Utilities\Source\Migration Tool\MigrationTool.exe";

How can I get just file name "MigrationTool.exe" by using regular expression?

Thanks

Lucy

Another regular expression question
user name
2006-07-10 06:30:18
Hi Lucy,

> I have file name, such as: 
> My $file =
"c:\views\12352_Utilities\na_gcs_12352fmcdealer\Ut
ilities\Source\Migration 
> Tool\MigrationTool.exe"; 

you did not try to print that one, true?
If you are using \ (the escape character) within
"" then it will just emphasize 
the next char, so your path will be without any \. You have
to write it using '' 
as delimiter or use duplicated \   (C:\\views.....)  or
just change the path 
delimiter to / (UNIX like).
The latter won't matter, PERL will treat the path correctly
on Windows even 
if it is a UNIX style path.

> How can I get just file name
"MigrationTool.exe" by using regular expression?


Now to your little problem:

parts = split (/[\\\/]/, $path);
$filename_with_suffix = pop (parts);

With the first line you tell PERL to generate the list of
the pathparts delimited 
by either '/' or '\'.
The second line will retrieve the last element of that list.
And the solution is using a reguar expression (/[\\\/]/)
in which the duplication 
of the escape character \ is used.

Have a nice day,
   Axel
_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Another regular expression question
user name
2006-07-10 13:14:59


my $file = "c:\views\12352_......\MigrationTool.exe";
my $filename = "";
if (-e $file) {
  ($filename = $file) =~ s%.*(\/|\\)(.*?)$%$2%;
  print "It is not file\n" unless ($filename);
} else {
  print "File does not exist\n";
}


----- Original Message ----
From: "Cai, Lucy (L.)" <lcai3ford.com&gt;
To: activeperllistserv.activestate.com
Sent: Friday, July 7, 2006 11:43:44 AM
Subject: Another regular expression question

Another regular expression<wbr> question

Hi,

I have file name, such as:

My $file = "c:\views\12352_Utilities\na_gcs_12352fmcdealer\Utilities\Source\Migration Tool\MigrationTool.exe";

How can I get just file name "MigrationTool.exe" by using regular expression?

Thanks

Lucy

_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )