simple regex problem

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default simple regex problem

    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 Guest

  2. Similar Questions and Discussions

    1. simple regex
      I've got a file with a list of ip's in brackets, fqdn's and -'s. How do you simply remove the 's and the -'s from the file with a simple perl...
    2. "Simple" regex advice (possibly '/' causing an issue?)
      Ian.H wrote: (snipped) You are trying to use Perl's $_ but it does not exist, for your context.
    3. [PHP] regex problem
      Merlin wrote: Are you making this too hard? if($string = 'test/contact.html') { echo 'good'; } else { echo 'bad'; } ??
    4. Simple regex question
      William S. Perrin wrote: You can try this: m/(\w+\b)\s+(+?)\s*/g (untested) -- Gunnar Hjalmarsson
    5. Simple Regex question (~[^newline characters])
      <? // Test example: echo '<pre>'; $aInput = ' abtgh kdghg 9867 jkuhkh ^ { ooijj 438764 - 56_ 67 '; $aInput = ereg_replace('', ',' ,...
  3. #2

    Default Re: simple regex problem

    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 Guest

  4. #3

    Default Re: simple regex problem

    On Mon, 08 Sep 2003 10:35:00 +0100, JS <vervoom@hotmail.com> 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 Guest

  5. #4

    Default Re: simple regex problem

    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: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  6. #5

    Default Re: simple regex problem

    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 Guest

  7. #6

    Default Re: simple regex problem

    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 Guest

  8. #7

    Default Re: simple regex problem

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    JS <vervoom@hotmail.com> wrote in news:bjhihj$nsq$1@cspc1n11.baplc.com:
    > 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+TqrfKseQRoKzuJw mmSa8An0yB
    JwDum1lkjJ/0GP4V/PI9z6uX
    =E/iX
    -----END PGP SIGNATURE-----
    Eric J. Roode Guest

  9. #8

    Default Re: simple regex problem

    JS <vervoom@hotmail.com> 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
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  10. #9

    Default Re: simple regex problem

    JS <vervoom@hotmail.com> 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
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  11. #10

    Default Re: simple regex problem

    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 Guest

  12. #11

    Default Re: simple regex problem

    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 Guest

  13. #12

    Default Re: simple regex problem

    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: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  14. #13

    Default Re: simple regex problem

    On Mon, 08 Sep 2003 10:35:00 +0100, JS wrote:
    > 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:
    I know its not a regex, but with variable dept name lenghts, why not:

    while (<DATA>) {
    my $f = 4;
    my @x = split /\s+/;
    my ($dept,$stats) = ( join(" ",@x[0 .. ($#x-$f)]), join(" ",@x[-($f) .. -1]) );
    print "Dept: $dept\tStat: $stats\n";
    }


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


    Dept: E-Test Stat: 3 - 4 -
    Dept: Health and Safety Stat: - 1 1 -
    Dept: Finance Stat: - 3 - -

    budman
    Buck Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139