I need to split the string
(age-is-25::OR::last_name-is-qa6)::AND::(age-is-20::OR::first_name-contains-test)
into
string[0] = (age-is-25::OR::last_name-is-qa6)
string[1] = AND
string[2] = (age-is-20::OR::first_name-contains-test)
I tried writing so many regex expressions, but nothing works as expected.
Using the following regex, Matcher.groupCount() which returns 2 but assigning results to an arraylist returns null as the elements.
Pattern pattern = Pattern.compile("(\\)::)?|(::\\()?");
I tried to split it using ):: or ::(.
I know the regex looks too stupid, but being a beginner this is the best I could write.
*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string. This regex pattern is not very useful. Let's take a regex pattern that may be a bit useful.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
If you want to match for the actual '+', '. ' etc characters, add a backslash( \ ) before that character. This will tell the computer to treat the following character as a search character and consider it for matching pattern. Example : \d+[\+-x\*]\d+ will match patterns like "2+2" and "3*9" in "(2+2) * 3*9".
You can use positive lookahead and lookbehind to match the first and last parentheses.
String str = "(age-is-25::OR::last_name-is-qa6)::AND::(age-is-20::OR::first_name-contains-test)"; for (String s : str.split("(?<=\\))::|::(?=\\()")) System.out.println(s);
Outputs:
(age-is-25::OR::last_name-is-qa6) AND (age-is-20::OR::first_name-contains-test)
Just a note however: It seems like you are parsing some kind of recursive language. Regular expressions are not good at doing this. If you are doing advanced parsing I would recommend you to look at other parsing methods.
To me it looks like a big part of your stress comes from the need for escaping special characters in your search term. I highly recommend to not do manual escaping of special characters, but instead to use Pattern.quote(...) for the escaping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With