Regular Expression $1?

Discussion in 'Perl Misc' started by Yang, Oct 10, 2007.

  1. Yang

    Yang Guest

    I want to get everything between () in the file, say (5), (6), so I
    wrote the following:

    while(my $line = <FIN>){
    chomp $line;
    if($line =~ /\(\d*\)/{
    print $1;
    }
    }

    But it always returns error saying that $1 is uninitialized.
    Yang, Oct 10, 2007
    #1
    1. Advertising

  2. Yang

    Jim Cochrane Guest

    On 2007-10-10, Yang <> wrote:
    > I want to get everything between () in the file, say (5), (6), so I
    > wrote the following:
    >
    > while(my $line = <FIN>){
    > chomp $line;
    > if($line =~ /\(\d*\)/{
    > print $1;
    > }
    > }
    >
    > But it always returns error saying that $1 is uninitialized.
    >


    I think you want:

    if ($line =~ /(\(\d+\))/ {
    ....
    }


    [CE: not tested.]

    --
    Jim Cochrane, Oct 10, 2007
    #2
    1. Advertising

  3. Jim Cochrane wrote:
    > On 2007-10-10, Yang <> wrote:
    >
    >>I want to get everything between () in the file, say (5), (6), so I
    >>wrote the following:
    >>
    >>while(my $line = <FIN>){
    >> chomp $line;
    >> if($line =~ /\(\d*\)/{
    >> print $1;
    >> }
    >>}
    >>
    >>But it always returns error saying that $1 is uninitialized.
    >>

    >
    >
    > I think you want:
    >
    > if ($line =~ /(\(\d+\))/ {
    > ...
    > }


    ITYM "/\((\d*)\)/"

    Yang wanted "everything *between* ()".

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Oct 10, 2007
    #3
  4. Yang

    Paul Lalli Guest

    On Oct 9, 9:12 pm, Yang <> wrote:
    > I want to get everything between () in the file, say (5), (6), so I
    > wrote the following:
    >
    > while(my $line = <FIN>){
    > chomp $line;
    > if($line =~ /\(\d*\)/{
    > print $1;
    > }
    >
    > }
    >
    > But it always returns error saying that $1 is uninitialized.


    $1 is assigned to whatever is "captured" by parentheses in the pattern
    match. You have no capturing parentheses in the pattern match. You
    only have the litteral parenthses that you're trying to match. You
    need to surround what you want to capture with parentheses:

    /\((\d*)\)/

    Note that \d* means "0 or more digits" which means an empty () is a
    valid match for this expression. If you want to require at least one
    digit, change the * to a +.

    Paul Lalli
    Paul Lalli, Oct 10, 2007
    #4
  5. Yang

    Cloink Guest

    Paul - well done for answering lucidly, it doesn't happen often, esp
    not on Perl pages.
    Cloink, Oct 10, 2007
    #5
  6. Yang

    Jeff Stampes Guest

    Yang wrote:
    > I want to get everything between () in the file, say (5), (6), so I
    > wrote the following:


    Not exactly, the following doesn't compile.

    > while(my $line = <FIN>){
    > chomp $line;
    > if($line =~ /\(\d*\)/{
    > print $1;
    > }
    > }
    >
    > But it always returns error saying that $1 is uninitialized.


    It would be helpful to provide an example of your data as well.

    IAC, I won't give you any fish, but here's a line and a pole:

    You get the capture variables ($1, $2, $3, etc) by capturing things in
    sets of parentheses. Where are the parentheses in your regexp that
    would be doing the capturing?

    ~Jeff
    Jeff Stampes, Oct 16, 2007
    #6
    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. Keith-Earl
    Replies:
    1
    Views:
    450
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,287
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    844
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,948
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page