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

Expression ?

Status
Not open for further replies.

WintersMystic

Programmer
Aug 8, 2001
39
US
can someone tell me what this is doing?

if ($graphicsfile =~ /.*[\/\\](.*)/){
$output = $1;
} else {
$output = $graphicsfile;
}
 
$graphicsfile =~ /.*[\/\\](.*)/

....tries to pick out the file name alone from the variable $graphicsfile.

So if

$graphicsfile="/home/user1/image.gif"

the matching operation would yield 'image.gif' and store it in $1;


'perldoc perlfaq6' on your UNIX prompt has more on Regular Expressions.

HTH
C
"Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
If you are trying to pick the filename off of the end of the full path,

Code:
#!/usr/local/bin/perl
$f = '/httpd/htdocs/temp/file.gif';
$f =~ s/.*[\/](.*$)/$1/;
#       |   |  | |   |
#       |   |  | |   put stuff in parens back
#       |   |  | pin the match to the end of the string
#       |   |  '.' is a wild card, '*' is one or more
#       |   either '\' or '/'
#     any number or anything
print "$f\n";

Note that in the [], the slashes are not metachars.
They get evaluated literally, so you don't need to
escape them.

Also, the parens in the match pattern catch whatever is inside in $1
which is then used to put the filename back in the replace.

The '$' pins the match to the end of the string. So, the match
works from the right end to the left. First it looks for any number
of anything '.*'. Just previous to that there must be either a
forward slash or a backslash.

HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top