You need to sign in to do that
Don't have an account?

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.
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.
All Answers
below code will help you
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.
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 :)
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
2.using string.split method split the file content in to list<string>, and iterate each line
//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());
}
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());
}
}
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...