moderated >> \1 doesn't match.

by baumann@pan » Thu, 02 Aug 2007 14:11:06 GMT

I want to use \1 to match the first grouped items, but it doesn't
work.

my $a = "000abc00";

if ($a =~ /([0-9]*)abc\1/)
{
print "matched $1 \n";
}
else
{
print "unmatched $1 \n";
}

the output is matched 00.

the expected result should be "unmatched 000".

moderated >> RE: \1 doesn't match.

by Sastien Nadeau » Thu, 02 Aug 2007 21:26:31 GMT



It looks like the regex engine goes down the string, finds 000, can't find a corresponding \1, then backtracks to the beginning, matches 00 (because that's one of the things that [0-9]* allows), goes down the string again and then finds 00. Its is happy, exits with a true value and fills $1 with the last match.

Sastien

moderated >> \1 doesn't match.

by Gunnar Hjalmarsson » Thu, 02 Aug 2007 21:48:12 GMT


As it should be...

The regex drops the first '0', or else it wouldn't match at all.


If the regex match had failed, $1 would have been undefined.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

moderated >> \1 doesn't match.

by Jim Gibson » Fri, 03 Aug 2007 00:13:06 GMT

In article < XXXX@XXXXX.COM >,



Three points:

1. If you want your match to start at the beginning of your string you
must anchor the pattern with ^ or \A.

2. If you want your grouped item to have more than zero characters,
then use + instead of * for a repeat quantifier character.

3. If there is not a match, then there will be nothing valid in $1.

Untested:

if ($a =~ /^([0-9]+)abc\1/)
{
print "matched $1 \n";
}
else
{
print "No match\n";
}

moderated >> \1 doesn't match.

by Ilya Zakharevich » Fri, 03 Aug 2007 04:01:15 GMT

[A complimentary Cc of this posting was sent to
Jim Gibson
< XXXX@XXXXX.COM >], who wrote in article <020820070913060006% XXXX@XXXXX.COM >:





More anchored is still needed for \1. Like (?<![0-9])\1$.

And if one does not want a fixed string "abc", one needs more
anchoring for the leading (); in this case, one does not need
look-ahead, it is enough to use (?>).

$a = "000abc0000";
if ($a =~ /^((?>[0-9]+)).*(?<![0-9])\1$/)
{
print "matched $1 \n";
}
else
{
print "No match\n";
}

Hope this helps,
Ilya

moderated >> \1 doesn't match.

by Dr.Ruud » Wed, 08 Aug 2007 17:01:59 GMT

baumann@pan schreef:


It *does* work, but not in the way you erroneously suspect it.



No, it shouldn't. You should do it in two steps, first capture the
prefix, then test the postfix.

--
Affijn, Ruud

"Gewoon is een tijger."

Similar Threads

1. Recover doesn't match regular expression - Perl

2. Doesn't match executable version

I run perldoc in windows, it shows the following error, please ask how
to solve it, thanks!

Perl lib version (v5.6.1) doesn't match executable version (v5.8.8) at
\product\10.1.0\Db_1\perl\5.6.1\lib\MSWin32-x86/Config.pm line 21.
Compilation failed in require at c:/Perl/lib/Pod/Perldoc.pm line 7.
BEGIN failed--compilation aborted at c:/Perl/lib/Pod/Perldoc.pm line 7
Compilation failed in require at c:\Perl\bin/perldoc.bat line 22.
BEGIN failed--compilation aborted at c:\Perl\bin/perldoc.bat line 22.

3. DBD::mysql::st execute failed: Column count doesn't match value count at row 1 a - Perl

4. :mysql::st execute failed: Column count doesn't match value count at row 1 a

5. regular expression that doesn't match any string - Perl

6. regular expression that doesn't match any string [repost as thread root]

Reposted as new thread:

[Note:  I am reposting this as a new message, in order to start a new thread.
Please remember to use the new message command, rather than a reply, when
starting a new subject.  For many of us with threaded browsers, your message got
lost way out on an unrelated thread.--Joseph]

Hi all
Just for fun (really no context) i was wondering how to create regular
expressions that
will never match any string.

/^[^\w\W]/ is one of them am I right? No string can start with a character
that is neither alphanumeric nor  nonalhanumeric.

But why does
/^[^.\n]/ matches .
doesn't that mean the string should begin a character that is not  any
character or not a newline(nothing else should be left).

Could someone enligthen me?
Below is to code I use to check.
thanks
oznur

use strict;
use warnings;

my $string="does it match";

if ($string=~/(^[^\w\W])/){
  print "matches $1\n";
}
else  {
  print "doesn't match \n";
}

if ($string=~/(^[^.\n])/){
   print "matches $1\n";
}
else  {
   print "doesn't match \n";
}



7. Perl lib version (v5.6.0) doesn't match executable version (v5.8.0) - Perl

8. Why doesn't %+ match %- for this regex?