function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kevoharakevohara 

Pattern and Matcher Help

Admittedly, I am not very good at regular expressions.  I was wondering if I could get some help on this.

 

I have an application that brings back a mathematical expression in the form of a string.  Example:

 

 

String test = '(25.4 + 87.2) / (23.8 -1)';

I am trying to match each sub expression contained within the parentheses and calculate those value.  So I am trying to use Pattern/Matcher to find those sub-expressions and cant seem to get the results I am looking for.  Here is what I have so far...

 

 

 

String test = '(25.4 + 87.2) / (23.8 - 1)';

Pattern patt = Pattern.compile('\\((.*?)\\)');

Matcher matcher = patt.matcher(test);

System.debug('MATCHES: ' + matcher.Matches());
System.debug('GROUP COUNT: ' + matcher.groupCount());

 

I would need both of the expressions in the test returned.  The regex that I believe I need is...

 

\((.*?)\)

 

 

I have tested this regex on http://gskinner.com/RegExr/ and it works fine.  I am not getting any result using the Pattern/Matcher.

 

Any assistance would be appreciated.

 

Thanks