java - Regex with -, ::, ( and ) -
i need split 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 many regex expressions, nothing works expected.
using following regex, matcher.groupcount() returns 2 assigning results arraylist returns null elements.
pattern pattern = pattern.compile("(\\)::)?|(::\\()?");
i tried split using ):: or ::(.
i know regex looks stupid, being beginner best write.
you can use positive lookahead , lookbehind match first , last parentheses.
string str = "(age-is-25::or::last_name-is-qa6)::and::(age-is-20::or::first_name-contains-test)"; (string s : str.split("(?<=\\))::|::(?=\\()")) system.out.println(s);
outputs:
(age-is-25::or::last_name-is-qa6) , (age-is-20::or::first_name-contains-test)
just note however: seems parsing kind of recursive language. regular expressions not @ doing this. if doing advanced parsing recommend @ other parsing methods.
Comments
Post a Comment