Skip to main content
Show correct debug output ;)
Source Link
Phil W
  • 38.1k
  • 5
  • 53
  • 106

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:

\[(fact\.[^]]*)]

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]');

System.debug(myMatcher);

if (myMatcher.find(0)) {
    System.debug(myMatcher.group(1));
}

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

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:

\[(fact\.[^]]*)]

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]');

System.debug(myMatcher);

if (myMatcher.find(0)) {
    System.debug(myMatcher.group(1));
}

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

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:

\[(fact\.[^]]*)]

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]');

System.debug(myMatcher);

if (myMatcher.find(0)) {
    System.debug(myMatcher.group(1));
}

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
Source Link
Phil W
  • 38.1k
  • 5
  • 53
  • 106

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:

\[(fact\.[^]]*)]

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]');

System.debug(myMatcher);

if (myMatcher.find(0)) {
    System.debug(myMatcher.group(1));
}

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