Back references

Discussion in 'Perl Misc' started by Frieza, Nov 14, 2007.

  1. Frieza

    Frieza Guest

    Hi I am studying Perl on my ciw course, I was wondering if anyone
    could
    tell me what back references are in regular expressions in plain
    simple english, cause my course notes that I have, I need a dictionary
    everytime I read a sentence.
    Thanks
    Frieza, Nov 14, 2007
    #1
    1. Advertising

  2. Frieza wrote:
    > Hi I am studying Perl on my ciw course, I was wondering if anyone
    > could
    > tell me what back references are in regular expressions in plain
    > simple english, cause my course notes that I have, I need a dictionary
    > everytime I read a sentence.


    Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
    et al. They're very good textbooks absolutely stuffed with plain simple
    english.

    Backreferences are the $1..$9 variables defined after a regular
    expression has been applied to a string. They contain the substrings
    which match the bits in parentheses. For example:

    #!/usr/bin/perl
    use strict;

    my $string = "aaa bbb";
    $string =~ /(a*) (b*)/;
    print "\$1 = '$1' and \$2 = '$2'\n";


    Phil

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Nov 14, 2007
    #2
    1. Advertising

  3. Frieza

    Frieza Guest

    On 14 Nov, 19:50, Philip Potter <> wrote:
    > Frieza wrote:
    > > Hi I am studying Perl on my ciw course, I was wondering if anyone
    > > could
    > > tell me what back references are in regular expressions in plain
    > > simple english, cause my course notes that I have, I need a dictionary
    > > everytime I read a sentence.

    >
    > Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
    > et al. They're very good textbooks absolutely stuffed with plain simple
    > english.
    >
    > Backreferences are the $1..$9 variables defined after a regular
    > expression has been applied to a string. They contain the substrings
    > which match the bits in parentheses. For example:
    >
    > #!/usr/bin/perl
    > use strict;
    >
    > my $string = "aaa bbb";
    > $string =~ /(a*) (b*)/;
    > print "\$1 = '$1' and \$2 = '$2'\n";
    >
    > Phil
    >
    > --
    > Philip Potter pgp <at> doc.ic.ac.uk


    Thanks Phil that makes it alot clearer already, I will definetly look
    up those books by Larry Wall.
    Thanks
    Frieza, Nov 14, 2007
    #3
  4. Frieza

    Frieza Guest

    On 14 Nov, 19:56, Jim Gibson <> wrote:
    > In article <>,
    >
    > Frieza <> wrote:
    > > Hi I am studying Perl on my ciw course, I was wondering if anyone
    > > could
    > > tell me what back references are in regular expressions in plain
    > > simple english, cause my course notes that I have, I need a dictionary
    > > everytime I read a sentence.
    > > Thanks

    >
    > A back reference in a regular expression is a value that has been
    > matched and captured earlier ("back") in the pattern. Perl back
    > references are denoted by \1, \2, etc.
    >
    > For example, if you wish to match any two consecutive and identical
    > lower-case letters, you can use the regular expression:
    >
    > m/([a-z])\1/
    >
    > Note how this will differ from the regular expression
    >
    > m/[a-z][a-z]/
    >
    > which will match any two consecutive lower-case letters not necessarily
    > the same.
    >
    > See also
    >
    > perldoc perlretut
    > perldoc perlre
    >
    > and search for 'backreference'.
    >
    > --
    > Jim Gibson
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com


    Jim thanks very much the added detail has helped alot.
    Thanks again
    Frieza, Nov 14, 2007
    #4
  5. Frieza

    Uri Guttman Guest

    >>>>> "PP" == Philip Potter <> writes:

    PP> Frieza wrote:
    >> Hi I am studying Perl on my ciw course, I was wondering if anyone
    >> could
    >> tell me what back references are in regular expressions in plain
    >> simple english, cause my course notes that I have, I need a dictionary
    >> everytime I read a sentence.


    PP> Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
    PP> et al. They're very good textbooks absolutely stuffed with plain simple
    PP> english.

    PP> Backreferences are the $1..$9 variables defined after a regular
    PP> expression has been applied to a string. They contain the substrings
    PP> which match the bits in parentheses. For example:

    that is incorrect. those are grabs and the scalar variables used to
    access them later on (in the replacement string or later).

    backreferences are when you refer to a previous grab INSIDE the same
    regex. $1 inside a regex will refer to a grab from an earler regex grab,
    not the current one. you use \1 to refer to the first grab in the
    current regex.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 14, 2007
    #5
  6. Frieza

    Uri Guttman Guest

    >>>>> "F" == Frieza <> writes:

    F> Thanks Phil that makes it alot clearer already, I will definetly look
    F> up those books by Larry Wall.

    and he was wrong. see my other post in this thread.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 14, 2007
    #6
  7. Philip Potter <> wrote:
    > Frieza wrote:
    >> Hi I am studying Perl on my ciw course, I was wondering if anyone
    >> could
    >> tell me what back references are in regular expressions in plain
    >> simple english, cause my course notes that I have, I need a dictionary
    >> everytime I read a sentence.

    >
    > Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall



    Only one of those was by Larry, the other one was by Randal et. al.


    > They're very good textbooks absolutely stuffed with plain simple
    > english.



    That part, at least, is accurate.


    > #!/usr/bin/perl
    > use strict;



    You should always enable warnings when developing Perl code.


    > $string =~ /(a*) (b*)/;
    > print "\$1 = '$1' and \$2 = '$2'\n";



    You should never use the dollar-digit variables unless you have
    first ensured that the match *succeeded*.

    if ( $string =~ /(a*) (b*)/ ) {
    print "\$1 = '$1' and \$2 = '$2'\n";
    }


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Nov 15, 2007
    #7
  8. Uri Guttman wrote:
    > that is incorrect. those are grabs and the scalar variables used to
    > access them later on (in the replacement string or later).
    >
    > backreferences are when you refer to a previous grab INSIDE the same
    > regex. $1 inside a regex will refer to a grab from an earler regex grab,
    > not the current one. you use \1 to refer to the first grab in the
    > current regex.


    Whoops! Thanks for the correction. (And thanks to Tad as well.)

    --
    Philip Potter pgp <at> doc.ic.ac.uk
    Philip Potter, Nov 15, 2007
    #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. Roger Leigh
    Replies:
    8
    Views:
    434
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    447
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    710
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    833
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page