Click here to get back home

Assigning a value to pos to control regular expression matching

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Assigning a value to pos to control regular expression matching Mark 05-13-2008
Posted by Mark on May 13, 2008, 5:15 pm
Please log in for more thread options
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.

Posted by John W. Krahn on May 13, 2008, 5:31 pm
Please log in for more thread options
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

Posted by Gunnar Hjalmarsson on May 13, 2008, 5:34 pm
Please log in for more thread options
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

Posted by xhoster on May 13, 2008, 5:38 pm
Please log in for more thread options
>
> 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.

Posted by Mark on May 13, 2008, 5:43 pm
Please log in for more thread options
> use strict ;
> use warnings ;
>
> my $txt =3D 'abc Start something' ;
> my $startpoint =3D index($txt,'Start') ;
> die "Start point not found" if $startpoint < 0 ;
>
> $_ =3D $txt ;
>
> pos =3D $startpoint ;
> if (/\G(.*)/g) {
> =A0 =A0 print "matched=3D$1\n" ; # prints "matched=3DStart something"}
>
> else {
> =A0 =A0 print "no match\n" ; }
>
> pos =3D $startpoint ;
> if ($txt =3D~ /\G(.*)/g) {
> =A0 =A0 print "matched=3D$1\n" ; # prints "matched=3Dabc Start something"
>
> }
>
> else {
> =A0 =A0 print "no match\n" ;
>
> }
>
> I expected the matched part in both cases to be the same. =A0The second
> case doesn't seem to honor the value of pos.
>
> Can someone please explain.

Thank you to all that responded.

Similar ThreadsPosted
regular expression, matching sub item January 30, 2006, 6:04 pm
matching a complicated url in a regular expression January 13, 2007, 12:32 pm
matching chunks of data with a regular expression August 26, 2004, 7:02 am
Regular Expression check for non matching string September 22, 2005, 8:27 am
Variable interpolation and m in regular expression matching January 22, 2008, 8:42 am
Matching multiple subexpressions in a regular expression March 11, 2008, 7:49 pm
Regular expression for matching words containing underscore _ character December 12, 2007, 10:27 am
regexp - display the last matching expression May 9, 2006, 6:35 am
Matching and Regular Expressions November 20, 2006, 11:44 am
matching HTML expression across multiple lines May 10, 2006, 4:31 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap