basic regex back reference

Discussion in 'Perl Misc' started by gcr, May 31, 2006.

  1. gcr

    gcr Guest

    Newbie alert. Thanks for any help.

    Can't get back references - I want to pull the bracketed numbers (and
    put them in an array)

    #!/usr/bin/perl

    use warnings;
    use strict;

    my $output = "5[64]790[908]90567[5678]101";
    $output =~ m/(\[+[*\d]+\])/g;
    print "$1\n";

    prints [64]

    was hoping to find [908] in $2 and [5678] in $3 ...
    gcr, May 31, 2006
    #1
    1. Advertising

  2. gcr wrote:
    > Newbie alert. Thanks for any help.
    >
    > Can't get back references - I want to pull the bracketed numbers (and
    > put them in an array)
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > my $output = "5[64]790[908]90567[5678]101";
    > $output =~ m/(\[+[*\d]+\])/g;
    > print "$1\n";
    >
    > prints [64]
    >
    > was hoping to find [908] in $2 and [5678] in $3 ...


    You can use a while loop to pull out subsequent matches (dunno if
    there's a proper word for that):

    use warnings;
    use strict;
    my @array;
    my $output = "5[64]790[908]90567[5678]101";
    while ($output =~ m/(\[+[*\d]+\])/g) {
    push @array, $1;
    }
    print join(",", @array), "\n";

    This will print
    [64],[908],[5678]

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, May 31, 2006
    #2
    1. Advertising

  3. gcr

    gcr Guest

    In article <252e$447d55a6$c2abfc64$>,
    Aukjan van Belkum <> wrote:

    > my @array = ($output =~ m/(\[+[*\d]+\])/g);


    works great! thanks!
    (I still don't understand but the pain is gone)

    The original only matches once(?)
    and/or I'm not understanding the $1 $2 functionality(?)

    Thanks again!
    gcr, May 31, 2006
    #3
  4. gcr

    Dr.Ruud Guest

    gcr schreef:

    > /(\[+[*\d]+\])/


    The "\[+" means: one or more literal "["s.

    The "[*\d]" is a character class off a literal asterisk and a digit, so
    it tries to match either an asterisk or a digit. The "+" behoind it
    means "one or more of the previous" again.

    ITYM: /(\[\d+\])/
    which means: a literal "[", followed by one or more digits, followed by
    a literal "]".

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 31, 2006
    #4
  5. gcr wrote:
    > In article <252e$447d55a6$c2abfc64$>,
    > Aukjan van Belkum <> wrote:
    >
    >>my @array = ($output =~ m/(\[+[*\d]+\])/g);

    >
    > works great! thanks!
    > (I still don't understand but the pain is gone)
    >
    > The original only matches once(?)
    > and/or I'm not understanding the $1 $2 functionality(?)


    $1 contains the match in the first set of parentheses and $2 contains the
    match in the second set of parentheses. You only have one set of parentheses
    in that pattern.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, May 31, 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. Joby
    Replies:
    0
    Views:
    1,627
  2. Engineer
    Replies:
    6
    Views:
    611
    Jeremy Bowers
    May 1, 2005
  3. Replies:
    0
    Views:
    427
  4. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  5. Dan Fitzpatrick

    Regex back reference in gsub

    Dan Fitzpatrick, Jul 13, 2005, in forum: Ruby
    Replies:
    4
    Views:
    363
    David A. Black
    Jul 13, 2005
Loading...

Share This Page