• 0

[Java] Pattern Help


Question

Hi

I am trying to use split in the String class. I am try to split a string at + but not a plus inside brackets

e.g (a+b+(c+d)) + (e - f)

I want the pattern to be able to split the above expression into (a+b+(c+d)) and (e - f).

Does anyone know how to construct or sugggest a pattern like that ??

Thanks

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

typical math parsing eh? :)

IIRC this is called tockenization - might be wrong though.

I'd suggest tackelnig is recursively.

ohh god my java is so gone...

find_end(string a, level n = 0){

if( contains (a.substring(')'), '(') )

  a = find_end (a.substring-after ('('), n+1);

return a.substring-after(')');

}

sorry. I'm not so awaik right now. I hope you get the idea though.

I't leaking parrallelism parsing too.

I've been doing this kind of stuff heavily the last week for my

latest project a PlainTextMarkupLanguage Parser in XSL

kindest regards,

Moritz "neofeed" Angermann

Link to comment
Share on other sites

  • 0

I'll try to tackel this later again if I find time... Sorry for the poor answer :/

kindest regards,

Moritz "neofeed" Angermann

Link to comment
Share on other sites

  • 0

There isn't a really easy way to do this with split(). The easiest thing to do is to parse the equation and generate a tree from it where the nodes of the tree are math signs (+, -) and the leaves are the variables. You can find lots of examples (recursive and non-recursive) from google; just look up "expression trees".

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.