Ruby Regex: parsing C++ classes -
i curious parsing c++ code using regexp. have far (using ruby) allows me extract class declarations , parent classes (if any):
/(struct|class)\s+([^{:\s]+)\s*[:]?([^{]+)\s*\{/
here example in rubular. notice can capture correctly "declaration" , "inheritance" parts.
the point @ stuck @ capturing class body. if use following extension of original regex:
/(struct|class)\s+([^{:\s]+)\s*[:]?([^{]+)\s*\{[^}]*\};/
then can capture class body only if not contain curly braces, , therefore class or function definition. @ point have tried many things none of them make better. instance, if include in regexp fact body can contain braces, capture first class declaration , subsequent classes if part of first class' body!
what missing?
the group capturing might help:
# named v backref v /(struct|class)\s+(?<match>{((\g<match>|[^{}]*))*})/m
here find matching curly bracket 1 following struct
/class
declaration. want tune regexp, posted make solution clear possible.
Comments
Post a Comment