Regular expressions

Discussion in 'Perl Misc' started by Jørn Dahl-Stamnes, Nov 7, 2006.

  1. I'm trying to extract parts of a string with the following line:

    $relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;

    But $relay contain data only if it match the first part of the expression.
    I have tested both parts of the expression, and they both work.

    Can the |-operator be used in the line above?

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
    Jørn Dahl-Stamnes, Nov 7, 2006
    #1
    1. Advertising

  2. Jørn Dahl-Stamnes

    Paul Lalli Guest

    Jørn Dahl-Stamnes wrote:
    > I'm trying to extract parts of a string with the following line:
    >
    > $relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;
    >
    > But $relay contain data only if it match the first part of the expression.
    > I have tested both parts of the expression, and they both work.
    >
    > Can the |-operator be used in the line above?


    The | can be used there, yes. The $1, however, cannot. $1 will ALWAYS
    refer to the first parentheses in the entire match, regardless of
    whether or not the sub-pattern that contains it was actually matched.
    You want the little known $+ variable instead. It's documented in

    perldoc perlvar

    $+ The text matched by the last bracket of the last
    successful search pattern. This is useful if you
    don't know which one of a set of alternative
    patterns matched. For example:

    /Version: (.*)|Revision: (.*)/ && ($rev = $+);


    Paul Lalli
    Paul Lalli, Nov 7, 2006
    #2
    1. Advertising

  3. Paul Lalli wrote:

    > Jørn Dahl-Stamnes wrote:
    >> I'm trying to extract parts of a string with the following line:
    >>
    >> $relay =~ s/^\[(.+)\]$|^(.+) \[.+$/$1/;
    >>
    >> But $relay contain data only if it match the first part of the
    >> expression. I have tested both parts of the expression, and they both
    >> work.
    >>
    >> Can the |-operator be used in the line above?

    >
    > The | can be used there, yes. The $1, however, cannot. $1 will ALWAYS
    > refer to the first parentheses in the entire match, regardless of
    > whether or not the sub-pattern that contains it was actually matched.
    > You want the little known $+ variable instead. It's documented in
    >
    > perldoc perlvar


    Thanks, it worked. I could not find this in the O'Reillys "Programming Perl"
    book I got. But then, I have not read every word in it.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
    Jørn Dahl-Stamnes, Nov 7, 2006
    #3
  4. Jørn Dahl-Stamnes

    Paul Lalli Guest

    Jørn Dahl-Stamnes wrote:
    > Paul Lalli wrote:
    >
    > > You want the little known $+ variable instead. It's documented in
    > >
    > > perldoc perlvar

    >
    > Thanks, it worked. I could not find this in the O'Reillys "Programming Perl"
    > book I got. But then, I have not read every word in it.


    Page 184 in the Regexp section on Caputring and Clustering, and page
    668 in the list of all special variables. In the 3rd edition, at
    least. . .

    Paul Lalli
    Paul Lalli, Nov 7, 2006
    #4
  5. Paul Lalli wrote:

    > Jørn Dahl-Stamnes wrote:
    >> Paul Lalli wrote:
    >>
    >> > You want the little known $+ variable instead. It's documented in
    >> >
    >> > perldoc perlvar

    >>
    >> Thanks, it worked. I could not find this in the O'Reillys "Programming
    >> Perl" book I got. But then, I have not read every word in it.

    >
    > Page 184 in the Regexp section on Caputring and Clustering, and page
    > 668 in the list of all special variables. In the 3rd edition, at
    > least. . .


    I only got the 2nd edition... :-(
    And it does not have any "Update book" function... guess I must buy a new
    version.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
    Jørn Dahl-Stamnes, Nov 8, 2006
    #5
  6. Jørn Dahl-Stamnes

    Paul Lalli Guest

    Jørn Dahl-Stamnes wrote:
    > Paul Lalli wrote:
    > > Page 184 in the Regexp section on Caputring and Clustering, and page
    > > 668 in the list of all special variables. In the 3rd edition, at
    > > least. . .

    >
    > I only got the 2nd edition... :-(
    > And it does not have any "Update book" function... guess I must buy a new
    > version.


    You should *really* *really* consider doing so. Perl has changed quite
    a bit since version 5.0 was released, which is what the 2nd ed of the
    Camel covers.

    Paul Lalli
    Paul Lalli, Nov 8, 2006
    #6
  7. Jørn Dahl-Stamnes

    Ben Morrow Guest

    Quoth "Paul Lalli" <>:
    >
    > Jørn Dahl-Stamnes wrote:
    > > Paul Lalli wrote:
    > > > Page 184 in the Regexp section on Caputring and Clustering, and page
    > > > 668 in the list of all special variables. In the 3rd edition, at
    > > > least. . .

    > >
    > > I only got the 2nd edition... :-(
    > > And it does not have any "Update book" function... guess I must buy a new
    > > version.

    >
    > You should *really* *really* consider doing so. Perl has changed quite
    > a bit since version 5.0 was released, which is what the 2nd ed of the
    > Camel covers.


    OTOH, Perl has also changed quite a bit since 5.6.0, which is what the
    Blue Camel covers. While I learnt masses about Perl from reading the
    Camel through, I don't find I refer to it at all now: the perldocs are
    more useful, and always up to date.

    My suggestion would be to get a copy of the 3rd ed. from a library and
    read it through, to get some idea of what has changed. Note that in
    particular all of the 'upcoming' Unicode stuff and all of the IO layers
    stuff has changed quite significantly since 5.8.

    Ben

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Nov 8, 2006
    #7
  8. Ben Morrow wrote:
    > Quoth "Paul Lalli" <>:
    >>Jørn Dahl-Stamnes wrote:
    >>>Paul Lalli wrote:
    >>>>Page 184 in the Regexp section on Caputring and Clustering, and page
    >>>>668 in the list of all special variables. In the 3rd edition, at
    >>>>least. . .
    >>>I only got the 2nd edition... :-(
    >>>And it does not have any "Update book" function... guess I must buy a new
    >>>version.

    >>You should *really* *really* consider doing so. Perl has changed quite
    >>a bit since version 5.0 was released, which is what the 2nd ed of the
    >>Camel covers.

    >
    > OTOH, Perl has also changed quite a bit since 5.6.0, which is what the
    > Blue Camel covers.


    Both the 2nd and 3rd editions have blue covers.


    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Nov 9, 2006
    #8
    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. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    597
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,711
  3. Dustin D.
    Replies:
    1
    Views:
    11,142
  4. Jay Douglas
    Replies:
    0
    Views:
    592
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page