Need help on building regex

Discussion in 'Perl Misc' started by doni, Dec 21, 2006.

  1. doni

    doni Guest

    Hi all,

    I'm new to perl & also new to regex. Could u all please help me
    building a regex to do the following:

    I have text files with lines like these:
    BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

    I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
    e, f are numerics.

    Any help will be much appreciated.
    doni, Dec 21, 2006
    #1
    1. Advertising

  2. doni

    tfe Guest

    Hello,

    if the numbers are at the end of each line , you can use: the regular
    expression /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/

    you will get ($1,$2,$3,$3,$4,$5,$6) containing a b c d e f.


    doni a écrit :

    > Hi all,
    >
    > I'm new to perl & also new to regex. Could u all please help me
    > building a regex to do the following:
    >
    > I have text files with lines like these:
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    >
    > I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
    > e, f are numerics.
    >
    > Any help will be much appreciated.
    tfe, Dec 21, 2006
    #2
    1. Advertising

  3. doni

    Mirco Wahab Guest

    doni wrote:
    > I'm new to perl & also new to regex. Could u all please help me
    > building a regex to do the following:
    >
    > I have text files with lines like these:
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    >
    > I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
    > e, f are numerics.


    You wont need a regex for fixed-format stuff
    like that, a simple 'split' will do.

    You'll need to learn:
    - 1) open a file
    - 2) read from the file
    - 3) split the line read into fields
    - 3) put the fields into an array

    Here's one Idea (try to figure out the steps):

    ---8<----

    use strict;
    use warnings;

    my $text = q'
    BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    ';

    my @array;

    open(my $fh, '<', \$text) or die "without a blink $!";
    while( <$fh> ) {
    push @array, [ (split) [5..10] ];
    }
    close $fh;

    for my $row (@array) {
    print join ',', @$row;
    print "\n";
    }
    ---8<----

    Regards

    Mirco
    Mirco Wahab, Dec 21, 2006
    #3
  4. doni

    Doni Wahyudi Guest

    many thanks tfe, it works.

    thanks to you too wahab. good idea..

    On Dec 21, 10:10 pm, "tfe" <> wrote:
    > Hello,
    >
    > if the numbers are at the end of each line , you can use: the regular
    > expression /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/
    >
    > you will get ($1,$2,$3,$3,$4,$5,$6) containing a b c d e f.
    >
    > doni a écrit :
    >
    > > Hi all,

    >
    > > I'm new to perl & also new to regex. Could u all please help me
    > > building a regex to do the following:

    >
    > > I have text files with lines like these:
    > > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    > > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

    >
    > > I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
    > > e, f are numerics.

    >
    > > Any help will be much appreciated.
    Doni Wahyudi, Dec 22, 2006
    #4
  5. doni

    Dr.Ruud Guest

    doni schreef:

    > I have text files with lines like these:
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
    >
    > I want to read the value of a, b, c, d, e, f into an array.
    > a, b, c, d, e, f are numerics.


    perl -wnle '
    $, = "," ;
    print +(/[0-9]+/g)[-6..-1]
    ' filename

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 22, 2006
    #5
    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. KraftDiner
    Replies:
    1
    Views:
    423
    Simon Forman
    Aug 4, 2006
  2. Student
    Replies:
    17
    Views:
    604
    EventHelix.com
    Mar 14, 2006
  3. Midex
    Replies:
    24
    Views:
    905
  4. Jeff Schiller

    Need Help Building PythonQt on Windows

    Jeff Schiller, Mar 9, 2008, in forum: Python
    Replies:
    4
    Views:
    1,207
    Martin v. Löwis
    Mar 9, 2008
  5. Replies:
    3
    Views:
    749
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page