Recursive regular expression oneliner


  • 0

    Ruby is one of the apparently few languages that support recursive regular expressions.

    def is_valid(s)
      !(s !~ /^((\(\g<1>\)|\[\g<1>\]|\{\g<1>\})*)$/)
    end
    

    The \g<1> recursively refers to the first group, i.e., everything between the ^ and the $.

    Using double negation to return true or false instead of match data or nil.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.