Python parsing bracketed blocks

后端 未结 9 1931
独厮守ぢ
独厮守ぢ 2020-11-27 04:44

What would be the best way in Python to parse out chunks of text contained in matching brackets?

\"{ { a } { b } { { { c } } } }\"

should i

9条回答
  •  一整个雨季
    2020-11-27 05:16

    Cleaner solution. This will find return the string enclosed in the outermost bracket. If None is returned, there was no match.

    def findBrackets( aString ):
       if '{' in aString:
          match = aString.split('{',1)[1]
          open = 1
          for index in xrange(len(match)):
             if match[index] in '{}':
                open = (open + 1) if match[index] == '{' else (open - 1)
             if not open:
                return match[:index]
    

提交回复
热议问题