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
Senthil 1Senthil 1 

Use of regEx in SFDC

Hi,
 I have the following code in my controller.
----------------------------------------------------
 Pattern findMethodPattern = Pattern.compile('/\bSREV_[a-zA-Z0-9]+Handler[.]([a-zA-Z0-9]+)(?=)/gi');
 string str = 'SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);';
 Matcher tgrBodyMatter = findMethodPattern.matcher(str);
 system.debug('>>>'+ tgrBodyMatter.matches());//is always false.
----------------------------------------------------
 From the above code, the tgrBodyMatter.matches() should return true and with a value "TaskBeforeUpdate" but it is always false. if I use the same input and expression is working fine on https://regex101.com/r/oJ8xL2/1
Please guide.

 
Best Answer chosen by Senthil 1
David ZhuDavid Zhu
Pattern findMethodPattern = Pattern.compile('SREV_TaskHandler[\\.]([a-zA-Z]+)[\\(]'); List trgList = [SELECT Name,status,Body From ApexTrigger WHERE Status in ('Active', 'Inactive')]; for(ApexTrigger tgr: trgList){ Matcher tgrBodyMatcher = findMethodPattern.matcher(string.valueOf(tgr.body)); if(tgrBodyMatcher.find()) { do { system.debug( '-->>' + tgrBodyMatcher.group(1) ); } while(tgrBodyMatcher.find()); } }

All Answers

Ramesh KosalairamanRamesh Kosalairaman
Hi 

below code will help you
Pattern findMethodPattern = Pattern.compile(
   '[SREV_^/]+'    
  + '('       //start capturing
  + '[^/]+' //one or more of anything 
  + 'Handler'//the search term escaped for regex
  + '[^/]+' //one or more of anything 
  + ')'       //stop capturing
);
 string str = 'SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);';
 Matcher tgrBodyMatter = findMethodPattern.matcher(str);
 system.debug('>>>'+ tgrBodyMatter.matches());

 
Senthil 1Senthil 1
Hi,
   Do I need to put all the values different lines? I can see multipe '+' above, if so that is also wrong... I tried to put the above expression in a single line but still the output is false? Did you try to execute the above in Developer Console?
Plz guide.

 
Ramesh KosalairamanRamesh Kosalairaman
That Plus I was used for string concat there is no problem will rise becasue of this
Senthil 1Senthil 1
Ok... Did you try to execute the above in Developer Console?
 
Ramesh KosalairamanRamesh Kosalairaman
yes buddy I tried this Thats why I shared 
Senthil 1Senthil 1
Ok... the following code is working. My bad, i missed the following bold line:
 
Pattern findMethodPattern = Pattern.compile(
   '[SREV_^/]+'   
  + '('       //start capturing
  + '[^/]+' //one or more of anything
  + 'Handler'//the search term escaped for regex
  + '[^/]+' //one or more of anything
  + ')'       //stop capturing
);
 
string str = 'SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);';
Matcher tgrBodyMatcher = findMethodPattern.matcher(str);
system.debug('>>matches='+ tgrBodyMatcher.matches());
if (tgrBodyMatcher.matches()) {
   system.debug('>>>>'+ tgrBodyMatcher.group(0));
}
 
The result I m now getting is "SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);" but expecting result is "TaskBeforeUpdate". Is there any way to modify the regEx to get the expected result? Meanwhile i m doing trial & error :)

 
 
 
David ZhuDavid Zhu
I think the regExp need to be changed and if grouping in regexpression, group(0) is always the original string. In my code below, group(1) is the one you expect. Please try.
 
​Pattern findMethodPattern = Pattern.compile('SREV_[^/]+\\.([^/]+)[\\(][^/]+');

string str = 'SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);';
Matcher tgrBodyMatcher = findMethodPattern.matcher(str);
system.debug('>>matches='+ tgrBodyMatcher.matches());

if (tgrBodyMatcher.matches()) {
   system.debug('>>>>'+ tgrBodyMatcher.group(0)); 
   system.debug('>>>>'+ tgrBodyMatcher.group(1));   // this will give you TaskBeforeUpdate
}


Senthil 1Senthil 1
HI David,
    The above code is working fine in a case if we have a single line string but the is not working if I have a file contant as below: 

trigger SREV_Task_TGR on Task (before Insert, before Update, After Update) 
{
 // if triggers are enabled
    if (SREV_Utility.isTriggerEnabled('EnableAllTriggers'))
    {
     if (Trigger.isBefore) 
        {
            if(Trigger.IsUpdate)
            {
               SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);
            }
            if(Trigger.IsInsert)
            {
                SREV_TaskHandler.TaskBeforeInsert(Trigger.new, null);
            }
        }
      if(Trigger.isAfter)
        {
            if(Trigger.IsUpdate)
            {
                  SREV_TaskHandler.TaskAfterUpdate(Trigger.new,Trigger.old);
            }
        }
    } 
}

The expected output should be
TaskBeforeUpdate
TaskBeforeInsert
TaskAfterUpdate

Another issue with the current regEx is, it will return me "isTriggerEnabled"  (see above underlined row) in a group because we are just considering SREV_
Please help.
Thanks in advance
Senthil 1Senthil 1
In fact I tried to use regEx as SREV_[^/]+\\.([^/]+)[\\(][^/]+/gi for global search but still not working.
David ZhuDavid Zhu
1. in pattern setting, change srev_ to srev_taskhandler
2.using string.split method split the file content in to list<string>, and iterate each line
David ZhuDavid Zhu
Pattern findMethodPattern = Pattern.compile('SREV_TaskHandler[\\.]([a-zA-Z]+)[\\(]');

//string str = 'SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);';

string str = 'if (SREV_Utility.isTriggerEnabled(EnableAllTriggers)) ' +
    '{ ' +
    ' if (Trigger.isBefore)  ' +
    '    { ' +
    '        if(Trigger.IsUpdate) ' +
    '        { ' +
    '           SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old); ' +
    '        } ' +
    '        if(Trigger.IsInsert) ' +
    '        { ' +
    '            SREV_TaskHandler.TaskBeforeInsert(Trigger.new, null); ' +
    '        } ' +
    '    }';


Matcher tgrBodyMatcher = findMethodPattern.matcher(str);

if(tgrBodyMatcher.find()) {
  do {
    system.debug( '-->>' + tgrBodyMatcher.group(1) );
  } while(tgrBodyMatcher.find());
}
Senthil 1Senthil 1
Hi David, 
    You are explicitly setting a name as marked in bold. This can  be any entity name. 
SREV_TaskHandler[\\.]([a-zA-Z]+)[\\(]

It may be working in case of string, but when dynamically fetched the trigger body its not working.
If possible, here is the entire code.
Create a trigger as below(the inner implementation is commented).

trigger SREV_Task_TGR on Task (before Insert, before Update, After Update) 
{/*
 // if triggers are enabled
    if (SREV_Utility.isTriggerEnabled('EnableAllTriggers'))
    {
     if (Trigger.isBefore) 
        {
            if(Trigger.IsUpdate)
            {
               SREV_TaskHandler.TaskBeforeUpdate(Trigger.new , Trigger.old);
            }
            if(Trigger.IsInsert)
            {
                SREV_TaskHandler.TaskBeforeInsert(Trigger.new, null);
            }
        }
      if(Trigger.isAfter)
        {
            if(Trigger.IsUpdate)
            {
                  SREV_TaskHandler.TaskAfterUpdate(Trigger.new,Trigger.old);
            }
        }
    } */
}

I have the following code in developer console.
Pattern findMethodPattern = Pattern.compile('SREV_TaskHandler[\\.]([a-zA-Z]+)[\\(]/gi');
List<ApexTrigger> trgList = [SELECT Name,status,Body From ApexTrigger WHERE Status in ('Active', 'Inactive')];

for(ApexTrigger tgr: trgList){
   Matcher tgrBodyMatcher = findMethodPattern.matcher(string.valueOf(tgr.body));
   if(tgrBodyMatcher.find()) {
  do {
    system.debug( '-->>' + tgrBodyMatcher.group(1) );
  } while(tgrBodyMatcher.find());

}


 
David ZhuDavid Zhu
Pattern findMethodPattern = Pattern.compile('SREV_TaskHandler[\\.]([a-zA-Z]+)[\\(]'); List trgList = [SELECT Name,status,Body From ApexTrigger WHERE Status in ('Active', 'Inactive')]; for(ApexTrigger tgr: trgList){ Matcher tgrBodyMatcher = findMethodPattern.matcher(string.valueOf(tgr.body)); if(tgrBodyMatcher.find()) { do { system.debug( '-->>' + tgrBodyMatcher.group(1) ); } while(tgrBodyMatcher.find()); } }
This was selected as the best answer
Senthil 1Senthil 1
Hi Devid,
    This is what i wanted :)... I have made a change to regEx and now it is working fine... 

Pattern findMethodPattern = Pattern.compile('SREV_.*Handler[\\.]([a-zA-Z]+)[\\(]'); 
List<ApexTrigger> trgList = [SELECT Name,status,Body From ApexTrigger WHERE Status in ('Active', 'Inactive')]; 
for(ApexTrigger tgr: trgList){ 
    Matcher tgrBodyMatcher = findMethodPattern.matcher(string.valueOf(tgr.body)); 
    if(tgrBodyMatcher.find()) { do { system.debug( '-->>' + tgrBodyMatcher.group(1) ); } 
                               while(tgrBodyMatcher.find()); } }

Thanks a lot,
Regards...