I was not trying to come up with an altenative regex to do what he might have wanted (first 7 characters or filename up to the underscore)

I was trying to understand what "[^/]*" did in your regex. I know that you said that it was to make sure that there was not any "/" after the 7 characters. However I could not see the mechanics on how it would work.

Basically, I wanted a regex which would simply extract a pathname from a fully qualified filename and determine if "[^/]*" was needed to obtain the complete pathname.

So I modified your regex to extract the pathname and throw away the basename ... this showed me that
"[^/]*" was not required to traverse up to the last "/". The " .*/" part of the regex is greedy and grabs everything up to and including the last "/". This meant that "[^/]*" could be replaced with ".*" in your regex and still do the same thing...



----- Original Message ----- From: "Chris Devers" <[EMAIL PROTECTED]>
To: "Bernard Kenik" <[EMAIL PROTECTED]>
Cc: "Perl Beginners List" <[EMAIL PROTECTED]>
Sent: Monday, October 25, 2004 12:30 AM
Subject: RE: how the print the first 7 letter of file name



On Sun, 24 Oct 2004, Bernard Kenik wrote:

The part that puzzled me was [^/] .. so I experimented a little.  I asked
myself how I would extract the path portion.

Wasn't the requirement to *throw away* the path portion, not save it?

The final answer I came up with was: ( my $path = $file ) =~ s#(.*/).*#$1#;
This extract all characters up to and including the last slash since this is a
greedy regex.

Wouldn't that save the path, but not the filename?

Isn't that the opposite of what the guy was asking for?

Therefore I concluded that "[^/]" would not be needed.

( my $name = $file ) =~ s#.*/(.......).*$#$1#; does the same thing as ( my
$name = $file ) =~ s#.*/(.......)[^/]*$#$1#;

Maybe so, but when I was testing it on the command line, the variant you show was matching the first slash and the first 7 characters after it, which isn't what the guy (seemed to be) asking for.

That said, I'm still not sure I had it right. He said he wanted the
first seven characters, which is what I was trying to write, but it
looks like he wanted the part of the filename before the underscore:

 $no_path_file = "NewProcess_date_22-oct-2004.log";
 ( my $prefix = $no_path_file ) =~ s/_.*//; print $prefix;
 print $prefix;

-- prints "NewProcess", which seems to be what he really wanted.

But he *still* never described the goal clearly, so that's as close as I
care to bother getting :-)

This is not meant as a criticism but simply to show that someone was
watching and learning.

We're all learning, hopefully -- I know I am... :-)



--
Chris Devers


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to