\1 doesn't match.

View: New views
5 Messages — Rating Filter:   Alert me  

\1 doesn't match.

by baumann@pan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


RE: \1 doesn't match.

by Sébastien Nadeau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

baumann@pan wrote:

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


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.

Sébastien


Re: \1 doesn't match.

by Gunnar Hjalmarsson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

baumann@pan wrote:

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

As it should be...

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

> the expected result should be "unmatched 000".

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

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


Re: \1 doesn't match.

by Jim Gibson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In article <1186035066.962909.213410@...>,
<"baumann@pan"> wrote:

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

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";
}

Re: \1 doesn't match.

by Ilya Zakharevich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

[A complimentary Cc of this posting was sent to
Jim Gibson
<jgibson@...>], who wrote in article <020820070913060006%jgibson@...>:
> 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";
> }

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

LightInTheBox - Buy quality products at wholesale price!