simple regex problem

Discussion in 'Perl Misc' started by JS, Sep 8, 2003.

  1. JS

    JS Guest

    Hi,

    I'm trying to build a regex to put the department name into a variable
    $dept and the rest of the line into another variable $stats:

    E-Test 3 - 4 -
    Health and Safety - 1 1 -
    Finance - 3 - -

    This is my regex:

    ($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

    but it doesn't work because of the \s- is not actually handled as a
    string, but individual charaters.

    Can anyone fix this for me please?

    Thanks,

    JS.
    JS, Sep 8, 2003
    #1
    1. Advertising

  2. JS wrote:
    >
    > I'm trying to build a regex to put the department name into a variable
    > $dept and the rest of the line into another variable $stats:
    >
    > E-Test 3 - 4 -
    > Health and Safety - 1 1 -
    > Finance - 3 - -
    >
    > This is my regex:
    >
    > ($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;
    >
    > but it doesn't work because of the \s- is not actually handled as a
    > string, but individual charaters.
    >
    > Can anyone fix this for me please?



    my ( $dept, $stats ) = unpack 'A24 A*', $_;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2003
    #2
    1. Advertising

  3. JS

    Matija Papec Guest

    On Mon, 08 Sep 2003 10:35:00 +0100, JS <> wrote:

    >I'm trying to build a regex to put the department name into a variable
    >$dept and the rest of the line into another variable $stats:
    >
    >E-Test 3 - 4 -
    >Health and Safety - 1 1 -
    >Finance - 3 - -
    >
    >This is my regex:
    >
    >($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;


    untested,

    my ($dept, $stats) = /(.+?[\d-]+)\s+(.*)/;
    Matija Papec, Sep 8, 2003
    #3
  4. John W. Krahn wrote:
    > JS wrote:
    >> I'm trying to build a regex to put the department name into a
    >> variable $dept and the rest of the line into another variable
    >> $stats:
    >>
    >>E-Test 3 - 4 -
    >>Health and Safety - 1 1 -
    >>Finance - 3 - -

    >
    > my ( $dept, $stats ) = unpack 'A24 A*', $_;


    That doesn't work for the E-Test line. How about:

    my ($dept, $stats) = /(.+[a-z])\s+(.*)/;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 8, 2003
    #4
  5. JS

    JS Guest

    John W. Krahn wrote:
    > JS wrote:
    >
    >>I'm trying to build a regex to put the department name into a variable
    >>$dept and the rest of the line into another variable $stats:
    >>
    >>E-Test 3 - 4 -
    >>Health and Safety - 1 1 -
    >>Finance - 3 - -
    >>
    >>This is my regex:
    >>
    >>($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;
    >>
    >>but it doesn't work because of the \s- is not actually handled as a
    >>string, but individual charaters.
    >>
    >>Can anyone fix this for me please?

    >
    >
    >
    > my ( $dept, $stats ) = unpack 'A24 A*', $_;
    >
    >
    >
    > John


    Thanks John,

    The problem with that is if the dept name is longer than 24 characters e.g:

    Customer Service International
    JS, Sep 8, 2003
    #5
  6. JS

    JS Guest

    Gunnar Hjalmarsson wrote:
    > John W. Krahn wrote:
    >
    >> JS wrote:
    >>
    >>> I'm trying to build a regex to put the department name into a
    >>> variable $dept and the rest of the line into another variable
    >>> $stats:
    >>>
    >>> E-Test 3 - 4 -
    >>> Health and Safety - 1 1 -
    >>> Finance - 3 - -

    >>
    >>
    >> my ( $dept, $stats ) = unpack 'A24 A*', $_;

    >
    >
    > That doesn't work for the E-Test line. How about:
    >
    > my ($dept, $stats) = /(.+[a-z])\s+(.*)/;
    >


    Thank Gunnar,

    That works if I do:

    my ($dept, $stats) = /(.+[a-zA-Z])\s+(.*)/;
    JS, Sep 8, 2003
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    JS <> wrote in news:bjhihj$nsq$:

    > Hi,
    >
    > I'm trying to build a regex to put the department name into a variable
    > $dept and the rest of the line into another variable $stats:
    >
    > E-Test 3 - 4 -
    > Health and Safety - 1 1 -
    > Finance - 3 - -
    >
    > This is my regex:
    >
    > ($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;
    >
    > but it doesn't work because of the \s- is not actually handled as a
    > string, but individual charaters.
    >
    > Can anyone fix this for me please?


    Not until you explain how you can tell where the department name ends and
    the "rest of the line" begins.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP1xcbWPeouIeTNHoEQLAeQCgufJlY2+TqrfKseQRoKzuJwmmSa8An0yB
    JwDum1lkjJ/0GP4V/PI9z6uX
    =E/iX
    -----END PGP SIGNATURE-----
    Eric J. Roode, Sep 8, 2003
    #7
  8. JS <> wrote:

    > I'm trying to build a regex to put the department name into a variable
    > $dept and the rest of the line into another variable $stats:
    >
    > E-Test 3 - 4 -
    > Health and Safety - 1 1 -
    > Finance - 3 - -



    my($dept, $stats) = split /\s\s+/, $_, 2;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 8, 2003
    #8
  9. JS <> wrote:

    > The problem with that is if the dept name is longer than 24 characters e.g:



    What does the data look like when the dept name is longer than 24 characters?

    How can you tell where the longer-than-24-character name ends and
    the data in the following column begins?

    We do not have enough information to answer your (modified) question.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 8, 2003
    #9
  10. Gunnar Hjalmarsson wrote:
    >
    > John W. Krahn wrote:
    > > JS wrote:
    > >> I'm trying to build a regex to put the department name into a
    > >> variable $dept and the rest of the line into another variable
    > >> $stats:
    > >>
    > >>E-Test 3 - 4 -
    > >>Health and Safety - 1 1 -
    > >>Finance - 3 - -

    > >
    > > my ( $dept, $stats ) = unpack 'A24 A*', $_;

    >
    > That doesn't work for the E-Test line. How about:
    >
    > my ($dept, $stats) = /(.+[a-z])\s+(.*)/;


    Sorry, "doesn't work" is not enough information for me to fix the
    problem.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2003
    #10
  11. JS wrote:
    >
    > John W. Krahn wrote:
    > > JS wrote:
    > >
    > >>I'm trying to build a regex to put the department name into a variable
    > >>$dept and the rest of the line into another variable $stats:
    > >>
    > >>E-Test 3 - 4 -
    > >>Health and Safety - 1 1 -
    > >>Finance - 3 - -
    > >>
    > >>This is my regex:
    > >>
    > >>($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;
    > >>
    > >>but it doesn't work because of the \s- is not actually handled as a
    > >>string, but individual charaters.
    > >>
    > >>Can anyone fix this for me please?

    > >
    > > my ( $dept, $stats ) = unpack 'A24 A*', $_;

    >
    > The problem with that is if the dept name is longer than 24 characters e.g:



    my @fields = split /(\s{2,})/;
    my $dept = shift @fields;
    shift @fields;
    my $stats = join '', @fields;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2003
    #11
  12. John W. Krahn wrote:
    > Gunnar Hjalmarsson wrote:
    >>John W. Krahn wrote:
    >>
    >>>my ( $dept, $stats ) = unpack 'A24 A*', $_;

    >>
    >>That doesn't work for the E-Test line. How about:
    >>
    >> my ($dept, $stats) = /(.+[a-z])\s+(.*)/;

    >
    > Sorry, "doesn't work" is not enough information for me to fix the
    > problem.


    Didn't know it was your problem. ;-) Anyway, I tested your suggestion
    with OP's original example data, i.e. without changing the number of
    spaces:

    while (<DATA>) {
    my ( $dept, $stats ) = unpack 'A24 A*', $_;
    print "$dept\n$stats\n\n";
    }

    __DATA__
    E-Test 3 - 4 -
    Health and Safety - 1 1 -
    Finance - 3 - -

    +++++++++++++++++++++++++++
    Output:
    E-Test 3 -
    4 -

    Health and Safety
    - 1 1 -

    Finance
    - 3 - -
    +++++++++++++++++++++++++++

    Suppose that explains it.

    As others have pointed out, as long as we don't know for sure how you
    safely separate department name from the rest, we are still just guessing.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 8, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    999
  2. Phillip Vong

    Simple Validation Problem Using Regex

    Phillip Vong, Oct 4, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    383
    Phillip Vong
    Oct 4, 2006
  3. crybaby
    Replies:
    3
    Views:
    245
    Erik Jones
    Sep 21, 2007
  4. Replies:
    3
    Views:
    754
    Reedick, Andrew
    Jul 1, 2008
  5. Jim Kronhamn

    Regex problem, probably simple

    Jim Kronhamn, May 14, 2007, in forum: Ruby
    Replies:
    5
    Views:
    100
    Giles Bowkett
    May 17, 2007
Loading...

Share This Page