File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Parsing using regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing using regular expression" Watch "Parsing using regular expression" New topic

Parsing using regular expression

Kowshik Nandagudi
Ranch Hand

Joined: Dec 09, 2010
Posts: 57
Hi All,

I need to parse a formula which can have comments. A valid formula will be something like this.
B = rescale(A,'monthly') +33 ; 'this is a comment

The formula should end with semicolon ";" and a comments can start with single quote(').

If a formula does not end with semi colon then i should throw an Exception (Syntax error)

Invalid formula :
B = rescale(A,'monthly') +33 'this is a formula which does not terminate with semicolon

I need to strip the 'this is a comment in the above example.

I tried with the regular expression like this ['](.*) to strip but it also strips the 'monthly') +33

Can anybody help me in doing this using regular expression?

Some of the valid example of formula in my project

B = abs(A);
A = 2;
C =abs(A)+pow(A,2);

Thanks in advance
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Regular expressions are a bad tool for parsing like this. You need a proper parser, and that involves writing a grammar that specifies what is and what isn't allowed. JavaCC or ANTLR can be used for creating this parser.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Parsing using regular expression
It's not a secret anymore!