Need to write a regex in R - Perl which would split string on comma ',' but skip all instances of comma in between round parenthesis. The challenge is to ensure that the parentheses are balanced, i.e. close bracket maps back to its open bracket.
In the below regex code, everything works perfectly, except if you notice - Parentheses are not balanced, an inner end bracket is being considered for an outer start bracket
text <- "PEANUTS (PEANUTS, PEANUT OIL AND/OR COTTONSEED OIL AND/OR CANOLA OIL AND/OR SOYBEAN OIL, SALT), GOLDEN RAISINS (RAISINS, SULFUR DIOXIDE), DRIED CRANBERRIES (CRANBERRIES, SUGAR, CITRIC ACID, SUNFLOWER OIL (PROCESSING AID), ELDERBERRY JUICE CONCENTRATE (COLOR)), ALMONDS (ALMONDS, PEANUT OIL AND/OR COTTONSEED OIL AND/OR CANOLA OIL AND/OR SOYBEAN OIL, SALT), MACADAMIAS (MACADAMIAS, MALTODEXTRIN, SALT)"
strsplit(text, '\\([^*)^)]*\\)(*SKIP)(*F)|\\,', perl=T)
Using the above regex code, Dried Cranberries is not being splitted correctly. Please refer to the output screenshot here: Regex Code Output
Any help here would be much appreciated.. Thank you!