File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Using Java RegExpression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Java RegExpression" Watch "Using Java RegExpression" New topic
Author

Using Java RegExpression

ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
Hi Guys,

I am stuck with a problem where i need to parse in some formulas using java regular expression.
I have a formula as below


I am using |(pipe) symbol to match the pattern in pattern.compile.But when i have an OR operator within Formula (||) then it consider it as delimeter.and not as BITWISE operator.

My code snippet is given below :



When i pass the above given formula to this Pattern.compile then it dosenot work as required.

The output given is :




The expected Output is :




Can anyone help me to tweek the Pattern.compile to get the required output.
It works fine when their is no OR operator in the formula.....

Expecting some quick help from experts on this

Regards
Ashesh
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You need to use something as delimiter that can't occur in the actual data. If you're set on pipe symbols as delimiters, you could use three of them in a row (|||).
ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
Hello sir,

Could you please write down a sample pattern.compile example to achieve same ?

Regards
Ashesh
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Just use three pipes wherever you now use a single one.
ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
Hi sir,

I tried


It gives very different output as :



Everything is being read at g(1) itself and g(2) and g(3) is returned as null.

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It looks like you still have single pipes in your data. Also note that *all* pipe characters need to be escaped in the regexp, not just the first one in a sequence.
ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
But i am not looking for a solution by tweeking the input file.
As the formula is fetched from another XML file,it will difficult to replace | by ||| each time......Do we have any mechanism to achieve same by modifying the Pattern.Compile structure?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What you wish to do is not so important as what you actually can do:
You need to use something as delimiter that can't occur in the actual data.
ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
Any suggestion using the Regex Lazy quantifers?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You will not be able to solve this problem using regexps alone unless you have the delimiters changed along the lines of what I suggested. Please stop thinking otherwise now.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

ashesh sasidharan wrote:Any suggestion using the Regex Lazy quantifers?


Replace "." by "[^|]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

ashesh sasidharan wrote:I have a formula as below

I am using |(pipe) symbol to match the pattern in pattern.compile.But when i have an OR operator within Formula (||) then it consider it as delimeter.and not as BITWISE operator.

Well, if the input always contains 9 fields and the '||'s are only going to be in the last field, then I would suggest:
String[] fields = line.split("|", 9);

Alternatively, split the string and combine elements that are separated by an empty string.

Winston

[Edit:] A third option (possibly best) - preprocess the line, viz:the only possible problem with this approach is that you have to find a character (or combination) that cannot appear in your input.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
ashesh sasidharan
Greenhorn

Joined: Dec 07, 2011
Posts: 7
Hi Raymond,

Your solutions works great.....
Thanks guys for all your valuable suggestions......

Regards
Ashesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Java RegExpression