1

I have a use case where I want to search for a operator amongst <,>,<=,>=, and = in a given string and split the expression into 2 parts i.e. the right expression and the left expression and evaluate them individually before evaluating the final conditional operator.

This can be understood from the example below:

Pattern pattern1 = Pattern.compile("(.*?)(<|>|<=|=|>=)(.*)");
Matcher matcher2 = pattern1.matcher("4>=5");
while (matcher2.find()) {
        System.out.println(matcher2.group(1) + ";" + matcher2.group(2)+ ";" + matcher2.group(3));
}

Output:

4;>;=5

The expected output was 4;>=;5 but the >= operator got split because of the presence of the operator > independently.

I want to evaluate the clause (<|>|<=|=|>=) in a greedy fashion so that >= gets treated as a single entity and gets listed down if they occur together.

5
  • 3
    Just put them in the right order ... (<=|>=|=|>|<). That should do what you want. Commented Oct 26, 2012 at 20:27
  • @RobertHanson - Thats a good one. It worked but what if I am getting a list of these operators from a list. In that case, I may not be sure of the order. How do I make it independent of the order? Commented Oct 26, 2012 at 20:28
  • @RobertHanson: You should post that as an answer! Commented Oct 26, 2012 at 20:30
  • 1
    @AbhishekJain - You could sort them by length first. E.g. Collections.sort(listOfOperators, lengthComparator), where lengthComparator is an instance of Comparator that has been implemented to sort by length. Commented Oct 27, 2012 at 19:59
  • @RobertHanson - Thanks for suggesting that but I have already implemented the same using the same technique. Anyways, thanks for posting this. :) Commented Oct 27, 2012 at 20:20

2 Answers 2

2

you can try simplifying to

 pattern1 = Pattern.compile("(.*?)(>=?|<=?|=)(.*)");
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Nice simplification rather. But this doesn't work for 4=5.
@shyam - what if I am getting the operator list from an unsorted data structure. I may not be able to express the same without knowing all the operators.
@AbhishekJain Then you should not use regular expressions. What if one of your operators had a character that has special meaning as a regex?
@RohitJain Thanks :) missed the = in all those pipes
1
String testt = "4>=5";
System.out.println(testt.replaceAll("(.*?)(>=?|<=?|=)(.*)", "$1;$2;$3"));

Easy to understand and you will replace all at once. You had a mistake that would stop getting <= if it finds a < before it, so just place those 2 <= and >= to the first places.

8 Comments

same comment as I made to other answers as well : what if I am getting the operator list from an unsorted data structure. I may not be able to express the same without knowing all the operators.
So why not make a (operator1|operator2) string (runtime) and then attach it to the regex? "(.*?)"+operators+"(.*)"
exactly what I am trying to do but 1) I may not be sure of the order in which the operators come in from the unsorted data structure so, the order may well be <,>,<=,>= and thus the same problem. 2) I want to make it a dynamic thing where the operators may not be inter-related.
Yes, even I was thinking about that option. Let me give it a try. :)
But this would be an ugly implementation. Can we not do something smarter?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.