Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perl bug??? #384

Closed
p5pRT opened this issue Aug 16, 1999 · 3 comments
Closed

Perl bug??? #384

p5pRT opened this issue Aug 16, 1999 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 16, 1999

Migrated from rt.perl.org#1219 (status was 'resolved')

Searchable as RT1219$

@p5pRT
Copy link
Author

p5pRT commented Aug 16, 1999

From Dan.Reidy@wl.com

Script

#!/opt/perl5/bin/perl

#
# I expect to find 232, but I find 222
#
$string="123ABC2222321212";
print "string=$string\n";
  $string =~ /([0-9])([^\1])\1/;
  print "Match=$&\n";
  print "Before=$`\n";
  print "After=$'\n\n";
#
# just to prove the 2nd term doesn't mean "not 1"
# (it finds 212)
#
$string="123ABC2122321212";
print "string=$string\n";
  $string =~ /([0-9])([^\1])\1/;
  print "Match=$&\n";
  print "Before=$`\n";
  print "After=$'\n\n";

#
# A non-digit test. It finds 2X2
#
$string="123ABC2X22321212";
print "string=$string\n";
  $string =~ /([0-9])([^\1])\1/;
  print "Match=$&\n";
  print "Before=$`\n";
  print "After=$'\n";

Output

string=123ABC2222321212
Match=222
Before=123ABC
After=2321212

string=123ABC2122321212
Match=212
Before=123ABC
After=2321212

string=123ABC2X22321212
Match=2X2
Before=123ABC
After=2321212

Daniel J. Reidy
Warner Lambert Company
201 Tabor Road
Morris Plains, NJ 07950
(973) 540-7979
Dan.Reidy@​wl.com <mailto​:Dan.Reidy@​wl.com>

@p5pRT
Copy link
Author

p5pRT commented Aug 16, 1999

From @tamias

On Mon, Aug 16, 1999 at 04​:20​:10PM -0400, Reidy, Dan wrote​:

I am looking to find pattern like 121 232 191 or 454, but not 111 222 333
etc.
i.e. a 3 digit number that starts and ends with the same digit, and has a
different digit in between

$string =~ /\(\[0\-9\]\)\(\[^\\1\]\)\\1/;

This is not a bug in Perl, but a misunderstanding of character classes.
Character classes are compiled along with the regex, and are constant while
the regex is being applied. You cannot define a character class that
changes during 'runtime' for the regex.

[^\1] matches any character other than "\1", which can also be written as
"\001", "\x01", or "\cA".

Use a negative lookahead assertion instead​:

$string =~ /([0-9])(?!\1)(\d)\1/;

If you're not certain whether something is a Perl bug, comp.lang.perl.misc
is generally a good place to make sure, before reporting it to the perlbug
list. Additionally, if you wish to report a bug in Perl, please choose a
more descriptive subject line than "Perl bug???", which would apply to any
bug report. In this case, something like "Can't use backrefs inside char
classes" would have been appropriate.

HTH
Ronald

@p5pRT
Copy link
Author

p5pRT commented Aug 16, 1999

From [Unknown Contact. See original ticket]

"Reidy" == Reidy, Dan <Dan.Reidy@​wl.com> writes​:

  Reidy> I am looking to find pattern like 121 232 191 or 454, but
  Reidy> not 111 222 333 etc. i.e. a 3 digit number that starts and
  Reidy> ends with the same digit, and has a different digit in
  Reidy> between

  Reidy> # I expect to find 232, but I find 222

  Reidy> $string =~ /([0-9])([^\1])\1/;

Dan,

the [...] character class RE matches each character in it literally
(see perldoc perlre).

[^\1] matches NOT 1, the \ character redundantly escapes the 1.

Your RE will match any three-digit palindrome with no 1 in the middle.

One solution (to be tested with perl -n) to the exercise left to the
reader is​:

if (/([0-9])([0-9])\1/ and ($1 ne $2)) {
  print $&;
}

You got to try a little harder to find a real perl bug!

Adrian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant