Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

palindrome using regular expressions

by szabgab (Priest)
on Oct 10, 2006 at 10:53 UTC ( #577368=perlquestion: print w/ replies, xml ) Need Help??
szabgab has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to understand how Regexp::Common finds palindromes. I took out the code from the module that looks like this:
use re 'eval'; my $ch = '[a-zA-Z]'; my $palindrome; my $r = "(??{\$palindrome})"; $palindrome = qr/$ch|($ch)($r)?\1/; while (<>) { print if /^$palindrome$/; }
Would someone care to exmplain how it works? Are there other solutions not using (??{ code }) ?

Comment on palindrome using regular expressions
Download Code
Re: palindrome using regular expressions
by Hofmator (Curate) on Oct 10, 2006 at 11:28 UTC
    This uses the recursive definition of a palindrome:
    • A single letter is a palindrome.
    • Or a palindrome consists of a single letter, an (optional) palindrome and the same single letter.

    As for other solutions, there is of course the non-regex way of doing join '',reverse split //, $_ eq $_ but that is probably not what you are looking for.

    Note that my $r = "(??{\$palindrome})"; is equivalent to my $r = '(??{$palindrome})';, there is no reference involved (as I thought at first glance).

    Update: Minor typo correction

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      You don't have to split. reverse can also handle strings:

      #!/usr/bin/perl use strict; use warnings; my $palindrome = 'lagerregal'; my $check = reverse $palindrome; print "yes\n" if $check eq $palindrome;
      (see perldoc -f reverse)
        Yes, you are absolutely right. Thanks for the correction.

        In fact, I also thought that reverse worked on strings and so I tried print reverse 'hello' which returns hello. From that I (incorrectly) concluded that it doesn't work ... well, got bitten by list context again, print scalar reverse 'hello' does the job OK :)

        -- Hofmator

        Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: palindrome using regular expressions
by Anonymous Monk on Oct 10, 2006 at 11:56 UTC
    You cannot avoid using (??{ $code }) in a meaningful way. You need recursion, which means you either use (??{ $code }), or (?{ $code }), with the latter solution being more complicated.

    Of course, if you don't have to restrict yourself to regexes, it's trivial to avoid (??{ $code }). if ($str eq reverse $str) will do.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://577368]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-23 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (212 votes), past polls