Assigning a value to pos to control regular expression matching

M

Mark

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.
 
J

John W. Krahn

Mark said:
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
 
G

Gunnar Hjalmarsson

Mark said:
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.
 
X

xhoster

Mark said:
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.
 
M

Mark

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top