I have following expression:
String formula7 = "(^TBC10.Actual.Value_<<Year>> == \"Final\" ? ^FN10101.Actual.Value_<<Year>> : ^INT805.Consensus.Value_<<Year>>) - (^PH2003 + ^PH2005 + ^PH2011 + ^PH2010 + ^PH2837 + ^PH2838 + ^PH2839 + ^PH2006 + ^PH2089)"; Now I want to extract groups from this string like the following:
// First bracket 1.( ^ TBC10.Actual.Value_ << Year >> == "Final" ? ^ FN10101.Actual.Value_ << Year >> : ^ INT805.Consensus.Value_ << Year >> ) // Condition 2. TBC10.Actual.Value_ << Year >> == "Final" // Condition left side 3. TBC10.Actual.Value_ << Year >> // Condition right side 4. Final // Trure condition value 5. FN10101.Actual.Value_ << Year >> // False condition value 6. INT805.Consensus.Value_ << Year >> // Other part 7. - ( ^ PH2003 + ^ PH2005 + ^ PH2011 + ^ PH2010 + ^ PH2837 + ^ PH2838 + ^ PH2839 + ^ PH2006 + ^ PH2089) To achieve this, I made the following regular expression
// Start with any characters. Then space. But it is optional. Then == sign. Then space. But it is optional. // Then contain any characters. Then space. But it is optional. Then contains ?. Then space. But it is // optional. Then contain any characters. Then space. But it is optional. Then contains : Then space. But it // Is optional. Then contain any characters. Then any characters. But it is optional public static final String CONDITION_REGEX = "((((.*)\\s?==\\s?(.*))\\s?\\?\\s?(.*)\\s?:\\s?(.*))(.*)?)"; public static final Pattern CONDITION_PATTERN = Pattern.compile(CONDITION_REGEX); But when I run it, I am getting the following output
// replace caret(^), double comma(") and any white space from the string String formula = formula7.replaceAll("[\\^\"\\s]", ""); Matcher matcher = CONDITION_PATTERN.matcher(formula); if (matcher.matches()) { // (TBC10.Actual.Value_<<Year>>==Final?FN10101.Actual.Value_<<Year>>:INT805.Consensus.Value_<<Year>>)-(PH2003+PH2005+PH2011+PH2010+PH2837+PH2838+PH2839+PH2006+PH2089) String group1 = matcher.group(1); // (TBC10.Actual.Value_<<Year>>==Final?FN10101.Actual.Value_<<Year>>:INT805.Consensus.Value_<<Year>>)-(PH2003+PH2005+PH2011+PH2010+PH2837+PH2838+PH2839+PH2006+PH2089) String condition = matcher.group(2); // (TBC10.Actual.Value_<<Year>>==Final String conditionLeftSide = matcher.group(3); // (TBC10.Actual.Value_<<Year>> String conditionRightSide = matcher.group(4); // Final String trueCondition = matcher.group(5); // FN10101.Actual.Value_<<Year>> String condition6 = matcher.group(6); // INT805.Consensus.Value_<<Year>>)-(PH2003+PH2005+PH2011+PH2010+PH2837+PH2838+PH2839+PH2006+PH2089) String condition7 = matcher.group(7); // "" String condition8 = matcher.group(8); } What I am doing wrong and how can I correct the Regex to achieve the result? I think () represent groups in Regex. And for each group I used ().