Assigning a value to pos to control regular expression matching

Discussion in 'Perl Misc' started by Mark, May 13, 2008.

  1. Mark

    Mark Guest

    use strict ;
    use warnings ;

    my $txt = 'abc Start something' ;
    my $startpoint = index($txt,'Start') ;
    die "Start point not found" if $startpoint < 0 ;

    $_ = $txt ;

    pos = $startpoint ;
    if (/\G(.*)/g) {
    print "matched=$1\n" ; # prints "matched=Start something"
    }
    else {
    print "no match\n" ; }

    pos = $startpoint ;
    if ($txt =~ /\G(.*)/g) {
    print "matched=$1\n" ; # prints "matched=abc Start something"

    }
    else {
    print "no match\n" ;
    }

    I expected the matched part in both cases to be the same. The second
    case doesn't seem to honor the value of pos.

    Can someone please explain.
     
    Mark, May 13, 2008
    #1
    1. Advertising

  2. Mark wrote:
    > use strict ;
    > use warnings ;
    >
    > my $txt = 'abc Start something' ;
    > my $startpoint = index($txt,'Start') ;
    > die "Start point not found" if $startpoint < 0 ;
    >
    > $_ = $txt ;
    >
    > pos = $startpoint ;
    > if (/\G(.*)/g) {
    > print "matched=$1\n" ; # prints "matched=Start something"
    > }
    > else {
    > print "no match\n" ; }
    >
    > pos = $startpoint ;


    You are now matching with the $txt variable instead of the $_ variable
    so that should be:

    pos( $txt ) = $startpoint;

    > if ($txt =~ /\G(.*)/g) {
    > print "matched=$1\n" ; # prints "matched=abc Start something"
    >
    > }
    > else {
    > print "no match\n" ;
    > }
    >
    > I expected the matched part in both cases to be the same. The second
    > case doesn't seem to honor the value of pos.
    >
    > Can someone please explain.



    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, May 13, 2008
    #2
    1. Advertising

  3. Mark wrote:
    > use strict ;
    > use warnings ;
    >
    > my $txt = 'abc Start something' ;
    > my $startpoint = index($txt,'Start') ;
    > die "Start point not found" if $startpoint < 0 ;
    >
    > $_ = $txt ;
    >
    > pos = $startpoint ;
    > if (/\G(.*)/g) {
    > print "matched=$1\n" ; # prints "matched=Start something"
    > }
    > else {
    > print "no match\n" ; }
    >
    > pos = $startpoint ;
    > if ($txt =~ /\G(.*)/g) {
    > print "matched=$1\n" ; # prints "matched=abc Start something"
    >
    > }
    > else {
    > print "no match\n" ;
    > }
    >
    > I expected the matched part in both cases to be the same.


    Try

    pos($txt) = $startpoint;

    in the second case.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 13, 2008
    #3
  4. Mark

    Guest

    Mark <> wrote:
    >
    > pos = $startpoint ;


    Using pos without an argument implicitly uses $_ as the argument.
    Thus this resets pos for the next time a regex is done on $_.

    Since you want to use $txt, not $_, for the next match,
    change that to:

    pos $txt = $startpoint;



    > if ($txt =~ /\G(.*)/g) {
    > print "matched=$1\n" ; # prints "matched=abc Start something"
    >
    > }
    > else {
    > print "no match\n" ;
    > }
    >
    > I expected the matched part in both cases to be the same. The second
    > case doesn't seem to honor the value of pos.
    >
    > Can someone please explain.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , May 13, 2008
    #4
  5. Mark

    Mark Guest

    On May 13, 2:15 pm, Mark <> wrote:
    > use strict ;
    > use warnings ;
    >
    > my $txt = 'abc Start something' ;
    > my $startpoint = index($txt,'Start') ;
    > die "Start point not found" if $startpoint < 0 ;
    >
    > $_ = $txt ;
    >
    > pos = $startpoint ;
    > if (/\G(.*)/g) {
    >     print "matched=$1\n" ; # prints "matched=Start something"}
    >
    > else {
    >     print "no match\n" ; }
    >
    > pos = $startpoint ;
    > if ($txt =~ /\G(.*)/g) {
    >     print "matched=$1\n" ; # prints "matched=abc Start something"
    >
    > }
    >
    > else {
    >     print "no match\n" ;
    >
    > }
    >
    > I expected the matched part in both cases to be the same.  The second
    > case doesn't seem to honor the value of pos.
    >
    > Can someone please explain.


    Thank you to all that responded.
     
    Mark, May 13, 2008
    #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. VSK
    Replies:
    2
    Views:
    2,303
  2. =?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:
    851
    Alan Moore
    Dec 2, 2005
  3. Rocco Melzian
    Replies:
    3
    Views:
    80
    Rocco Melzian
    Jul 15, 2003
  4. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
  5. prati
    Replies:
    0
    Views:
    450
    prati
    Oct 27, 2012
Loading...

Share This Page