On 18 May 2002 04:21, John W. Krahn [mailto:[EMAIL PROTECTED]] wrote:
> Bill Akins wrote:
> > 
> > Hello all,
> 
> Hello,
> 
> > I have a var, $DOC_NAME, holding a file name.  I need to 
> get the first
> > part of the variable into another variable.  Some examples are
> > A98-12345, SO-02-789, P-99-029833 and GQE-37-2199.
> > 
> > Examples:
> > A98-12345, I need A98
> > SO-02-789, I need SO-02
> > P-99-029833 I need P-99
> > GQE-37-2199 I need GQE-37
> > 
> > I know if it were always the first X that I needed I could do this:
> > $SUBDIR = (substr ($DOC_NAME, 0, X));
> > Really, I need everything until I get 2 digits in the new 
> var.  Anyone
> > want to take a stab at this?
> 
> $ perl -le'
> for my $DOC_NAME ( qw/A98-12345 SO-02-789 P-99-029833 GQE-37-2199/ ) {
>     my ($SUBDIR) = $DOC_NAME =~ /(.*)-/;
>     print $SUBDIR;
>     }
> '
> A98
> SO-02
> P-99
> GQE-37
> 

That's just grabbing everything up to the last hyphen, not what was asked
for.

To match everything up to and including a pair of digits you need to match
two digits... /\d\d/ should be a good starting point.

I assume you are interested in the *first* pair of digits, so we need to
match minimally.

Something like

  /.*?\d\d/

should be about right... quick test.

        #!perl
        use strict;
        use warnings;
        while (<DATA>) {
                /.*?\d\d/;
                print "$&\n";
        }
        __DATA__
        A98-12345
        SO-02-789
        P-99-029833
        GQE-37-2199

gives:

        A98
        SO-02
        P-99
        GQE-37

Clearly, in the real usage you'll capture and not use $&.

Richard Cox 
Senior Software Developer 
Dell Technology Online 
All opinions and statements mine and do not in any way (unless expressly
stated) imply anything at all on behalf of my employer


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to