1

I written a code to split the following string

(((OPERATING_CARRIER='AB') OR (OPERATING_CARRIER='EY') OR ((OPERATING_CARRIER='VA') AND ((FLIGHT_NO=604) OR ((FLIGHT_NO=603) AND (STOCK='9W'))))))

into following array of strings

OPERATING_CARRIER='AB'
OPERATING_CARRIER='EY'
OPERATING_CARRIER='VA'
FLIGHT_NO=604
FLIGHT_NO=603
STOCK='9W'

The code is

String sa="(((OPERATING_CARRIER='AB') OR (OPERATING_CARRIER='EY') OR ((OPERATING_CARRIER='VA') AND ((FLIGHT_NO=604) OR ((FLIGHT_NO=603) AND (STOCK='9W'))))))";
        Matcher m = Pattern.compile("\\w+\\s*=\\s*(?:'[^']+'|\\d+)").matcher(sa);
        //System.out.println("contains "+sa.contains("((("));
        Stack<String> in_cond = new Stack<String>();
        Iterator<String> iter = in_cond.iterator();
        String new_sa=sa;
        System.out.println("Indivisual conditions are as follows : ");
        while(m.find()) {
            String aMatch = m.group();
            // add aMatch to match list...
            System.out.println(aMatch);
            in_cond.push(aMatch);
        }
        System.out.println("End of Indivisual conditions");

But now in the input string, the "=" can also be "<>" or"<" or ">" or "LIKE" eg :

(((OPERATING_CARRIER<>'AB') OR (OPERATING_CARRIER LIKE'EY') OR ((OPERATING_CARRIER='VA') AND ((FLIGHT_NO<604) OR ((FLIGHT_NO>603) AND (STOCK='9W'))))))

What changes need to be done in the regex?

1 Answer 1

3

I guess there are simpler (and more readable) ways to do this :).

Use replaceAll() to replace all braces with empty String. Next split based on either AND or OR.

public static void main(String[] args) {

    String s = "(((OPERATING_CARRIER='AB') OR (OPERATING_CARRIER='EY') OR ((OPERATING_CARRIER='VA') AND ((FLIGHT_NO=604) OR ((FLIGHT_NO=603) AND (STOCK='9W'))))))";
    String[] arr = s.replaceAll("[()]+","").split("\\s+(OR|AND)\\s+");
    for (String str : arr) {
        System.out.println(str);
    }
}

O/P :

OPERATING_CARRIER='AB'
OPERATING_CARRIER='EY'
OPERATING_CARRIER='VA'
FLIGHT_NO=604
FLIGHT_NO=603
STOCK='9W'
Sign up to request clarification or add additional context in comments.

3 Comments

This Works perfectly for me . Just a doubt now, I have the strings OPERATING_CARRIER<>'AB' , OPERATING_CARRIER LIKE 'EY', FLIGHT_NO>603 in a string array and i have to split them into OPERATING_CARRIER,<>,AB and so on , how would i do that
@jason - I believe you were asking for a perfect solution.:P
Hi Folks I have used the following code and it works just fine for me: String input= "name = 'name_1' AND in_stock IN {'in_stock_1','in_stock_2'} AND ( price BETWEEN '01-jan-2015' and '31-may-2015' OR price = 'price_3' )"; String sa =input; String[] arr = sa.replaceAll("[()]+","").split("\\s*(\\sOR|\\sAND)\\s*"); for(int i=0;i<arr.length;i++) { System.out.println(arr[i]); }

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.