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 Regular expression to compare numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression to compare numbers" Watch "Regular expression to compare numbers" New topic
Author

Regular expression to compare numbers

chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
I have a method

compare(Object value, String searchTxt);

The value can either be a Number, Date, String.
The searchTxt can have values to compare Strings, Dates, Numbers. It also can have logical comparison operators like =, !=, >, <, >=, <= eg.

compare(numValue, "> 1000 <= 2000)

compare(dateValue, "> 2006/12/12 <= 2007/01/25)

compare(stringValue, "> abc <= xyz )

Can I have some regular expression to control this comparision and make my life little easier. Because spliting the string and checking for all possible options can be a tedious job.

Peace n Regards
chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
The searchTxt can have values like

"> 1000 <= 2000"

"> 2006/12/12 <= 2007/01/25"

"> abc <= xyz"

But I can check for the instanceof for the "value" and know that the searchTxt contains logical opearators plus (only Number or only Date or only String).

The real requirement is to comare the "value" according to the search string.

Say for eg:
if the method compare is invoked as compare(200, ">=100<500") it should return true

if the method compare is invoked as compare("xyz", ">=abc<xxx") it should return false

and so on...>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you're stuck with splitting the string into tokens and processing each token. You're lucky in that your syntax is really simple

operator value [ operator value ... ]

And you're right, the code to test the object type, then test each token can be annoyingly boring and repetitive. I'd be tempted to build a swarm of little tiny classes - only a few lines each - to execute the operations.

An operation might look like:

Did that make sense? I bet you could build this with not one "if" test. Could be lost o fun.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Perhaps if you transform:
to
and invoke a scripting engine like beanshell, to evaluate the expression?


http://home.arcor.de/hirnstrom/bewerbung
chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
Hi James,
Can you give an example in java please
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[chandrajeet padhy ]:

I have a method

compare(Object value, String searchTxt);


From what you say later, I think it might make a lot more sense to instead make three methods:

And if you must, you can have an Object version:

Within each of the other three methods, you will know whether you're dealing with a String, Number or Date, making the code in each individual method simpler.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Do those searchTxt objects *need* to be Strings? Where are they coming from?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
Hi Jim Yingst,

I dont have difficulty in identifying Date, Number or String.

My problem is actually inside the compare method. How can I?
As I said The searchText is a tuff thing. And James got it right. But I need a java based solution.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See if you can translate my babble into Java ... one line at a time should do it. We try to give really good hints here, but not complete solutions. If you take the effort to make some almost working code, we'll help you move it along. So jump in ... it's a lot more fun if you make it work yourself.
chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
I made it work this way. Any suggestions for improvement is welcomed.



ValueComparator looks as:-



And then different operations as per:



Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Kool. I bet you can generalize a bit more yet. Just playing thought experiments here - you can decide if it's worth trying. What if the interface for Operation was still two objects?

Then the Operation would have to get the two arguments into the proper format itself. That seems like a good place to create a date or a number. An abstract DateOperation could do the date manipulation and call an abstract compare( Date, Date ) method.

Your map of operations might include the type:

That would multiply the number of operations to types*operators. Ick. Maybe we could cut back to one operator class per type:

where the arguments tell what to return if the standard compareTo() returns <0, 0 or >0.

Any of that sound fun?
chandrajeet padhy
Greenhorn

Joined: Jul 26, 2004
Posts: 21
Oh yea. I thought of that to have only one method compare(obj, obj) in the Operation interface. But, that will lead upto creating types*operators number of classes like DateGT, DateLT, DateGEQ, DateLEQ, NumberGT, NumberLT...so on.
I just kept it simiplified to have only few Operation impls to handle all data types. Great going! Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular expression to compare numbers