Finding parenthesis via regular expression

邮差的信 提交于 2020-01-22 02:19:09

问题


I am not great with regular expressions. I am looking to find if a string contains "( ),[ ], { }". Note, I am not looking for the contents in the actual ( ), just to see if the string contains ( ) or { }, [ ].

I know if I do .scan, it will take any matches and create an array (which I want). I just don't know the expression.


回答1:


Use alternatives with a non-greedy match:

/\(.*?\)|\{.*?\}|\[.*?\]/

Without those question marks, the patterns .* would be "greedy", eg scanning "abc(def)ehi(jkl)mno" would find only one match of "(def)ehi(jkl)" (the .* would gobble up everything to the last close bracket), but using non-greedy .*? you would get two matches "(def)" and "(jkl)" as you would want.




回答2:


If you want to check for matching parens / brackets / braces, this will work:

/\(.*\)|\{.*\}|\[.*\]/

see here for more.

And this returns the matching portions of your string:

>> "alkd(jfla)kjd{adlkfj}alkjfd".scan(/\(.*\)|\{.*\}|\[.*\]/)
=> ["(jfla)", "{adlkfj}"]



回答3:


You said (in a comment) that you want to validate matching parentheses. However, "())(" contains two pairs of one open and one closed parentheses, but they are not matching.

To confirm the string contains at least one pair of matching parentheses, braces or brackets, and no non-matching pairs, you can use a stack.

def check_it(str)
  h = { '(' => ')', '{' => '}', '[' => ']' }
  hi = h.invert 
  at_least_one_pair = false
  stack = []
  str.each_char do |c|
    if h.key?(c)
      stack << c
      at_least_one_pair = true
    elsif hi.key?(c)
      return false unless stack.last == hi[c]
      stack.pop
    end
  end
  stack.empty? && at_least_one_pair
end

check_it "who (is) {that} at the {door}?" #=> true
check_it "who (is{ [that at] the} door)?" #=> true
check_it "who (is) )that( at the door?"   #=> false
check_it "who (is[ )that] at the door?"   #=> false
check_it "who (is that at the door?"      #=> false
check_it "who is that at the door"        #=> false


来源:https://stackoverflow.com/questions/20233722/finding-parenthesis-via-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!