Location:Home > 使用 Live Writer 在 oschina 上写博客

使用 Live Writer 在 oschina 上写博客

up vote 7 down vote favorite 1

This question comes in an attempt to understand one of the answer in : How to check that a string is a palindrome using regular expressions?

Answer given by Markus Jarderot is :


Can someone please explain, whats exactly happening here....i need to do similar in Perl, but not able to understand this solution!!!

PS : I am not very good in perl so please go easy ....and also "this can't be considered a regular expression if you want to be strict" - i read this line, so i am aware that this not regex strictly

regex perl palindrome share | improve this question edited Sep 12 '14 at 12:06 asked Mar 12 '14 at 10:59 NoobEditor 10k 5 31 65 9   Re "i need to do similar in perl", That is Perl. –  ikegami Mar 12 '14 at 11:09 1   Perl regex grammar is described in perlre. –  ikegami Mar 12 '14 at 11:10      See this explanation of Perl recursive regular expressions: rexegg.com/regex-recursion.html –  Barmar Mar 12 '14 at 11:25 2   Just FYI, you don't have to use a regex to check if a string is palindromic. The simple case is a one-liner that can be readily seasoned to taste: sub is_palindromic { $_[0] eq reverse $_[0] } –  Zaid Mar 12 '14 at 11:44 2   @NoobEditor, It wasn't a comment on the spelling. I meant that the posted code is Perl code, so the request for Perl code is ...odd. ( perl the executable or Perl the language would both make sense.) –  ikegami Mar 12 '14 at 12:36  |  show 4 more comments

2 Answers 2

active oldest votes up vote 13 down vote accepted
  • ^ - matches beginning of string
  • ( - starts capture group #1
  • (.) - matches any single character except a newline, save it in capture group #2
  • (?1) - recurse = replace this group with the entire regexp capture group #1
  • \2 - matches the same thing as capture group #2. This requires the first and last characters of the string to match each other
  • | - creates an alternative
  • .? - optionally matches any one character that isn't a newline - This handles the end of the recursion, by matching an empty string (when the whole string is an even length) or a single character (when it's an odd length)
  • ) - ends capture group #1
  • $ - matches end of string or before a newline at the end of the string.

The recursion (?1) is the key. A palindrome is an empty string, a 1-character string, or a string whose first and last characters are the same and the substring between them is also a palindrome.

share | improve this answer edited Mar 12 '14 at 13:23 ikegami 222k 7 147 334 answered Mar 12 '14 at 11:34 Barmar 312k 26 156 244      A 1-character string has its first and last characters the same. –  Barmar Mar 12 '14 at 11:56      recursion (?1) is what stumped me!! :\ –  NoobEditor Mar 12 '14 at 11:59 1   @NoobEditor, It's documented under the heading "(?PARNO) (?-PARNO) (?+PARNO) (?R) (?0)" –  ikegami Mar 12 '14 at 13:16      @ikegami : yeah....m following up on that...thankx for help!! :) –  NoobEditor Mar 13 '14 at 5:08 add a comment  |  up vote 3 down vote

It might be easier to understand with this analogous function, that does the same thing for arrays:

Recommend:Windows Live Writer介绍及相关问题解决


sub palindrome {
  if (scalar(@_) >= 2) {
    my $first_dot = shift;
    my $slash_two = pop;
    return $first_dot eq $slash_two && palindrome(@_);
  } else {
    # zero or one items
    return 1;

print "yes!\n" if palindrome(qw(one two three two one));
print "really?\n" if palindrome(qw(one two three two two one));

The (?1) notation is a recursive reference to the start of the first parenthesis in the regex, the \2 is a backreference in the current recursion to the (.). Those two are anchored at the start and end of 'whatever is matching at the current recursion depth', so everything else is matched at the next depth down.

ikegami suspects this is faster:

sub palindrome {
   my $next = 0;
   my %symbols;
   my $s = join '', map chr( $symbols{$_} ||= $next++ ), @_;
   return $s =~ /^((.)(?1)\2|.?)\z/s;
share | improve this answer edited Mar 12 '14 at 13:22 ikegami 222k 7 147 334 answered Mar 12 '14 at 11:32 bazzargh 1,553 7 12      @ikegami, the point of this answer was not speed, but clarity, for someone who doesn't understand that last line. –  bazzargh Mar 12 '14 at 16:46 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged regex perl palindrome or ask your own question.

Recommend:Windows Live Writer 写博客还挺方便

Windows Live Writer beta 2.0已经出来了。之前一直用这个客户端工具来写Windows Live Spaces的,现在发现CSDN BLOG也可以通过WLW来写了。只是需要手工配置一下: 选择metablogapi的service type。 在Remote posting URL for your weblog

Recommend:2009年用 Windows Live Writer 2009 为我们的博客园写博客(图)

在此之前没有使用过Windows Live Writer 写过博客,看到dudu推荐用Windows Live Writer ,今天也尝试用了一下,并把过程记录下来供大家分享讨论。 Windows Live 2009 在很多功能都得到了完善和改进,终于在各个产品线上有了竞争的资本,

Your Answer
(Ctrl+Enter To Post)   
    Copyright © 2015-2017 163JAVA.COM All Rights Reserved.