scala regex filter wrapped elements -


i have input in form of strings following

ellipse {     attribute = foo      ellipse {         attribute = foo         line {             attribute = foo             attribute = foo             attribute = foo         }         line {             attribute = foo             attribute = foo         }     } } 

basically 2d-elements, able hold other 2d-elements inside them. task write regex, can seperate parent-elements children, can parsed seperately. in case of:

rectangle1{     attribute = foo } ellipse1{     attribute = foo     ellipse{         rectangle{             attribute = foo         }     } } 

i want able regex.findallin(string) , have rectangle1 , ellipse1 strings, can parse them. im no expert regexes made attempt, fails of course:

i tried to:

(?s)(?!((ellipse|point|line) \\{)).+(ellipse|point|line) \\{.*\\}

get ellipses or points or lines, which

(?s)(?!((ellipse|point|line) \\{)).+(ellipse|point|line) \\{.*\\}

include something, but

(?s)(?!((ellipse|point|line) \\{)).+(ellipse|point|line) \\{.*\\}

don't

(?s)(?!((ellipse|point|line) \\{)).+(ellipse|point|line) \\{.*\\}

have 'ellipse {' or 'point {' above them,

but doesnt work...

there way want, said i'm not expert regexes. if have answer me, grateful explanation, since understand solution. thank in advance!

pure regex not fit task. have use recursive regex, , java (and hence scala) don't support them.

however, using scala, can take advantage of powerful parser combinator library:

object parsercombinator extends app javatokenparsers packratparsers {    case class attr(value:string)   case class fig2d(name:string, attrs:list[attr], children:list[fig2d])    def fig2d:parser[fig2d] = (ident <~ "{") ~ rep(attr) ~ (rep(fig2d) <~ "}") ^^ {     case name ~ attrs ~ children => fig2d(name, attrs, children)   }    def attr:parser[attr] = "attribute" ~> "=" ~> "\\s+".r ^^ attr.apply    def fig2dlist = rep(fig2d)    val input =     """       |rectangle1{       |    attribute = foo       |}       |ellipse1{       |    attribute = foo       |    ellipse{       |        rectangle{       |            attribute = foo       |        }       |    }       |}     """.stripmargin     println(parseall(fig2dlist, input)) } 

prints:

 [13.5] parsed: list(fig2d(rectangle1,list(attr(foo)),list()), fig2d(ellipse1,list(attr(foo)),list(fig2d(ellipse,list(),list(fig2d(rectangle,list(attr(foo)),list())))))) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -