code_junkie

How to check that a string is a palindrome using regular expressions?

Nov 11th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. (.)(.)21
  2.  
  3. ([a-z])([a-z])[a-z]21
  4.  
  5. $a = "teststring";
  6. while(length $a > 1)
  7. {
  8. $a =~ /(.)(.*)(.)/;
  9. die "Not a palindrome: $a" unless $1 eq $3;
  10. $a = $2;
  11. }
  12. print "Palindrome";
  13.  
  14. /^((.)(?1)2|.?)$/
  15.  
  16. (.?)(.?)(.?)(.?)(.?).?54321
  17.  
  18. bool isPalindrome(String s1)
  19.  
  20. {
  21.  
  22. String s2 = s1.reverse;
  23.  
  24. return s2 == s1;
  25. }
  26.  
  27. $re = qr/
  28. . # single letter is a palindrome
  29. |
  30. (.) # first letter
  31. (??{ $re })?? # apply recursivly (not interpolated yet)
  32. 1 # last letter
  33. /x;
  34.  
  35. while(<>) {
  36. chomp;
  37. say if /^$re$/; # print palindromes
  38. }
  39.  
  40. (?<N>.)+.?(?<-N>k<N>)+(?(N)(?!))
  41.  
  42. WHILE string.length > 1
  43. IF /(.)(.*)1/ matches string
  44. string = 2
  45. ELSE
  46. REJECT
  47. ACCEPT
  48.  
  49. /(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?987654321/
  50.  
  51. str == str.reverse ? true : false
Add Comment
Please, Sign In to add comment