Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Java RegExpression

 
ashesh sasidharan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2798
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sir,

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

Regards
Ashesh
 
Tim Moores
Bartender
Posts: 2798
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use three pipes wherever you now use a single one.
 
ashesh sasidharan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2798
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2798
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion using the Regex Lazy quantifers?
 
Tim Moores
Bartender
Posts: 2798
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashesh sasidharan wrote:Any suggestion using the Regex Lazy quantifers?


Replace "." by "[^|]
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ashesh sasidharan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raymond,

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

Regards
Ashesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic