Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created January 4, 2012 02:24
Show Gist options
  • Save JoshCheek/1558138 to your computer and use it in GitHub Desktop.
Save JoshCheek/1558138 to your computer and use it in GitHub Desktop.
palindrome regex
def find_longest_palindrome(string)
string.scan(/
(?<pair>.){0}
(?<palindrome>
\g<pair>
(\g<palindrome>||.)
\k<pair+1>
)
/x).map(&:last).sort_by(&:length).last
end
find_longest_palindrome("abacdfgdcabaypqqpy") # => "ypqqpy"
find_longest_palindrome("abacdfgdcabaypqqpy") == "ypqqpy" # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment