List Info

Thread: Regular expression ;-)




Regular expression ;-)
user name
2006-02-16 16:03:00
> -----Original Message-----
> From: activeperl-bounceslistserv.ActiveState.com 
> [mailto:activeperl-bounceslistserv.ActiveState.com]
On 
> Behalf Of Mark Knoop
> Sent: Thursday, February 16, 2006 09:11
> To: ActivePerllistserv.ActiveState.com
> Subject: RE: Regular expression 
> 
> Howard wrote:
> >
> > my ($basename) = $_ =~ / ^(.*)\.[^.]+$/;
> >
> 
> I presume the space       ^ there is an error. Even so
this 
> does not work on
> 123
> 
> $bill wrote:
> 
> > I'd probably do it the easy way and just remove
the ext:
> >
> > (my $file = $_) =~ s/\.[^.]*$//;
> > print "$file\n";
> > ____________________
> 
> I would do it this way too... but I have got a bit
sucked in 
> to trying to do
> it using the model that was initially suggested which
makes a 
> neat little
> puzzle. I am stumped though! Can anyone find a way
using the
> 
> > $_="123";
> > /(XX)YY/;
> > print "$1\n" ; # result:
"123";
> 
> form?
> 
> Or can someone prove that it can't be done?
> 
> Mark

sub Fparse($)
{
    my $Self = shift;
    my ($Fp, $Fn, $Fe, $Pos1, $Pos2);
    $Pos1 = rindex($Self,(index($Self,'/') > -1) ?
'/' : '\\');
# Find the last slash in the filename.
    $Pos2 = rindex($Self,'.');
# Find the last period in the filename.
    if ($Pos1 > 0) {$Pos1++; $Fp = substr($Self,0,$Pos1)}
else {$Fp =
undef; $Pos1 = 0}          # Extract the path if present.
    if ($Pos2 > 0) {$Fn = substr($Self,$Pos1,$Pos2 -
$Pos1); $Pos2++;
$Fe = substr($Self,$Pos2)} # Extract the name and extension.
    else {$Fn = substr($Self,$Pos1); $Fe = undef}
    return(($Fp,$Fn,$Fe));
}

sub Check($)
{
    my $Self = shift;
    print("\nChecking >$Self<\n");
    my ($Path, $Leaf, $Ext) = Fparse($Self);
    print("Path >$Path<\n") if
(defined($Path));
    print("Leaf >$Leaf<\n") if
(defined($Leaf));
    print("Ext  >$Ext<\n")  if
(defined($Ext));
    return(1);
}

Check('123');
Check('123.txt');
Check('123.txt.txt');

------------------------------------------------------------
------------
--------------------------------------
Outputs:

c:\temp>ptest

Checking >123<
Leaf >123<

Checking >123.txt<
Leaf >123<
Ext  >txt<

Checking >123.txt.txt<
Leaf >123.txt<
Ext  >txt<

Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake
St. Louis MO
- USA Central Time Zone
636-755-2652 fax 636-755-2503

dirk.bremernisc.coop
www.nisc.coop 

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

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