stan_d has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,

I need to form a regex to extract data [Se/0/0] & [Gi/0/0] from below:

e.g.
IF-abc/12 [Se/0/0] [Second data][third]
IF-abc/1 [Gi/0/0] [Second data ]

I have defined the regex as:
IF-(.*?)\[(.*?)\]

So i read the field as

Node= IF-(.*?)\[(.*?)\]

and output the data as $Node_2 which should give me the data I need.
Data = $Node_2

However i noticed it tends to match the '[third]' field or '[second]'. How do I get it to match the first occurence of [] ??

Replies are listed 'Best First'.
Re: Regex help
by choroba (Cardinal) on May 20, 2012 at 12:47 UTC
    You want the first square brackets. So tell Perl:
    /^[^[]*(\[[^]]+])/
Re: Regex help
by Kenosis (Priest) on May 20, 2012 at 14:58 UTC

    "In general, if you know what you want to keep, use a regexp. If you know what you want to get rid of, use split." From a PerlMonks' posting Understanding Split and Join.

    Given that, here's another option:

    use Modern::Perl; say map {(split)[1]} <DATA>; __DATA__ IF-abc/12 [Se/0/0] [Second data][third] IF-abc/1 [Gi/0/0] [Second data ]

    Output:

    [Se/0/0][Gi/0/0]

    Hope this helps!

    Update: However, if you're set on a regex for capturing that data, try the following instead of the split above:

    say /(\[.*?\])/ while <DATA>;

    Output:

    [Se/0/0] [Gi/0/0]
Re: Regex help
by CountZero (Bishop) on May 20, 2012 at 14:36 UTC
    Your regex works OK on the data you provided: you will find the first field in $2.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      The Perl that can be spoken of is not the Eternal Perl... (Update: An adaptation from the Tao Te Ching, Chapter 1.)

        In the same vein:
        The variable that can be named
        Is not the Eternal variable
        Variable references?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Regex help
by Anonymous Monk on May 20, 2012 at 08:43 UTC

    However i noticed it tends to match the '[third]' field or '[second]'. How do I get it to match the first occurence of [] ??

    Please write/run a 3 line program that demonstrates this