Blog Stats
  • Posts - 10
  • Articles - 0
  • Comments - 1
  • Trackbacks - 0

 

Saturday, January 17, 2009

Using REGEX to find text within brackets or parenthesis


REGEX is not exactly my favorite technology to have to work with, but there are times when it is definitely the right tool for the job. Recently I needed to parse a large text file that contained blocks of information within curly brackets - {}.

I found a great solution for the REGEX at:
http://blog.stevenlevithan.com/archives/balancing-groups

Note that balancing groups is only in the Microsoft REGEX classes... if you are using a different library you may have to follow an alternative, less powerful route where you know the number of levels that may be used: http://blog.stevenlevithan.com/archives/regex-recursion

KUDOS to Steve...

My final solution by the way was:
@"\{(?>[^{}]+|\{(?)|\}(?<-Depth>))*(?(Depth)(?!))\}"
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
 

 

Copyright © LEV