I want to get the sub string 'fact.cm_comp_sales_adjustment_factor__c.living_area_default_index_score__c' in the string '[subj.cm_parcel__c.sqft__c]+[comp.cm_sale__c.sqft__c])*fact.cm_comp_sales_adjustment_factor__c.living_area_default_index_score__c]' . But it can't return the true value. Anyone can help? Here is my code :

Pattern MyPattern = Pattern.compile('(?<=[)fact.+?(?=])');
Matcher MyMatcher = MyPattern.matcher('[subj.cm_parcel__c.sqft__c]+[comp.cm_sale__c.sqft__c])*[fact.cm_comp_sales_adjustment_factor__c.living_area_default_index_score__c]');

And the return value:

enter image description here

When I check my regular expression on Regex 101 tool. It still returns the correct value:

enter image description here

  • It's worth noting that there are different flavours of regex. Apex uses the Java regex flavour.
    – Phil W
    Jul 20, 2022 at 9:58

1 Answer 1


On the assumption that you are only interested in a value starting "fact." and delimited by surrounding square brackets, I would use a simpler pattern, of the form:


Note that I haven't bothered to consider any preamble in the string, so the Matcher.matches method isn't going to work. Instead I'm going to use Matcher.find, which can search from a given position to find a match, ignoring text before and after that match.

The above regex says:

Find a '[' followed by 'fact.' followed by any length of text made of any characters except ']' and ended with a ']'.

It is set to capture the text from 'fact.', ignoring the leading '[', through to the character just before the closing ']'.

The example code is:

Pattern myPattern = Pattern.compile('\\[(fact\\.[^]]*)]');
Matcher myMatcher = myPattern.matcher('[subj.cm_parcel__c.sqft__c]+[comp.cm_sale__c.sqft__c])*[fact.cm_comp_sales_adjustment_factor__c.living_area_default_index_score__c]');


if (myMatcher.find(0)) {

Recall that group(0) is always the entire pattern match, so here group(1) is the explicitly defined capture group (as described above).

The debug output is of the form:

09:03:41.25 (27652283)|USER_DEBUG|[4]|DEBUG|Matcher:[delegate=Matcher[pattern=\[(fact\.[^]]*)] region=0,131 lastmatch=]]
09:03:41.25 (28188623)|USER_DEBUG|[7]|DEBUG|fact.cm_comp_sales_adjustment_factor__c.living_area_default_index_score__c
  • Thanks, Phil! It works for me now Jul 21, 2022 at 10:14
  • Feel free to mark as best answer if you like. Ta.
    – Phil W
    Jul 21, 2022 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .