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
Revati BhavsarRevati Bhavsar 

Pattern Matching in apex class

Here is my Apex class for matching the Pan number format with user entered pan number.Getting an error how should I correct it?
public with sharing class PAN_Check
{
  public CustBusinessPro__Applicant__c applicant{get;set;}
  // List<CustBusinessPro__Applicant__c> panCardList;
  List <String> panCardList{get;set;}
  List <String> PanFormat{get;set;}
  public PAN_Check()
  {
    applicant = new CustBusinessPro__Applicant__c();
    panCardList = new List<String> ();
    PanFormat = new List<String> {'[A-Z]{5}[0-9]{4}[A-Z]{1}'} ;
  }
  
  public void PanCheck()
  {
   // panCardList = new List<CustBusinessPro__Applicant__c> ();
   // panCardList = [SELECT Id,CustBusinessPro__PAN__c FROM CustBusinessPro__Applicant__c WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
      panCardList.add(applicant.CustBusinessPro__PAN__c);
    System.debug('----------------------' + panCardList);
    pattern MyPattern = pattern.compile(PanFormat);
    matcher MyMatcher = MyPattern.matcher(panCardList);
    if(MyMatcher.matches())
    {
      System.debug(panCardList + '---------------Valid');
    }
    else
    {
      System.debug(panCardList + '---------------Invalid');
    }
  }
}


Error:

Error: Compile Error: Method does not exist or incorrect signature: pattern.compile(List<String>) at line 20 column 25
BALAJI CHBALAJI CH
Hi Revati Bhavsar,

I have gone through your code and I suppose that "PanFormat" which you have used is the expression in which the Customer's Pan Card should be entered. In that case, there is no need to take a List <String> type. Since it is been used in Pattern matching, it does not allow List types.
And to check the List of panCardList, you can loop it.

I just revised your code, please let Me know if it works for you.
public with sharing class PAN_Check
{
    public CustBusinessPro__Applicant__c applicant{get;set;}
    // List<CustBusinessPro__Applicant__c> panCardList;
    List <String> panCardList{get;set;}
    String PanFormat{get;set;}
    public PAN_Check()
    {
        applicant = new CustBusinessPro__Applicant__c();
        panCardList = new List<String> ();
        PanFormat = '[A-Z]{5}[0-9]{4}[A-Z]{1}';
    }
    
    public void PanCheck()
    {
        // panCardList = new List<CustBusinessPro__Applicant__c> ();
        // panCardList = [SELECT Id,CustBusinessPro__PAN__c FROM CustBusinessPro__Applicant__c WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
        panCardList.add(applicant.CustBusinessPro__PAN__c);
        System.debug('----------------------' + panCardList);
        
        for(string panCard : panCardList)
        {
            pattern MyPattern = pattern.compile(PanFormat);
            matcher MyMatcher = MyPattern.matcher(panCard);
            if(MyMatcher.matches())
            {
                System.debug(panCardList + '---------------Valid');
            }
            else
            {
                System.debug(panCardList + '---------------Invalid');
            }
        }
    }
}

 
Naval Sharma4Naval Sharma4
Hi Revati,

In compile method you have to pass a string as parameter.
From a different thread.

Prep work
Build the regular expression (there's lots of web resources on that, but use the Java versions as that's what Apex does under the hood).
Eg. \d+h \d+m
Use braces to define the parts you're interested in. (\d+)h (\d+)m
Turn it into a string by escaping any backslashes '(\\d+)h (\\d+)m'

Write the code
Create a Pattern object using your string
Use that to create a Matcher object.
Test to see if you got any matches using the Matcher's matches() function.
If you did, you can get the matches from the Matcher's group() function.
Pattern p = Pattern.compile('(\\d+)h (\\d+)m'); 
Matcher pm = p.matcher( yourString ); 
if( pm.matches() )
{ 
     hours = pm.group(1); 
     minutes = pm.group(2);
}


I'll leave it to you to declare hours and minutes, give them defaults, and to cast them into numerical types if you don't want them as strings.
You can, of course, chain the methods:
Matcher pm = Pattern.compile('(\\d+)h (\\d+)m').matcher( yourString );


but if you're doing it more than once, you're recompiling the regular expression over and over.


Thanks,
Naval
Revati BhavsarRevati Bhavsar
Hi,
    ThanksBalaji CH for the revision in code.The class is getting saved successfully.However,I am getting an error that  variable applicant.PAN__c  does not exist.Where CustBusinessPro__PAN__c is the API name of the field of the custom object with API name CustBusinessPro__Applicant__c.I also tried using applicant.PAN__c still getting same error.Here is the screenshot of the CustBusinessPro__PAN__c field.User-added image

Can you help please?
Thanks in advance!!!
BALAJI CHBALAJI CH
Hi Revati,

You mean to say that "CustBusinessPro__Applicant__c" is the API Name of your custom object which has a custom field with API Name "CustBusinessPro__PAN__c". In order to use that field in your class, you can try CustBusinessPro__Applicant__c.CustBusinessPro__PAN__c

Please let Me know if that works for you.

Best Regards,
BALAJI
Revati BhavsarRevati Bhavsar
Hi Balaji CH,
   I tried your suggestion,but got an error on following line:
       panCardList.add(CustBusinessPro__Applicant__c.CustBusinessPro__PAN__c);
Error: Compile Error: Incompatible element type Schema.SObjectField for collection of String at line 29 column 9

Regards,
Revati
 
BALAJI CHBALAJI CH
Hi Revati,

Can you please paste your entire code once again.
And also if possible screen shot of your Custom Object as I showed below.
Example Screen Shot of My Custom Object 

Best Regards,
BALAJI
Revati BhavsarRevati Bhavsar
Hi,
   Balaji CH here is the code and screenshots of my object and field I require to access.
public with sharing class PAN_Check

{

    public CustBusinessPro__Applicant__c applicant{get;set;}

    List <String> panCardList{get;set;}

    String PanFormat{get;set;}

    public PAN_Check()

    {

        applicant = new CustBusinessPro__Applicant__c();

        panCardList = new List<String> ();

        PanFormat = '[A-Z]{5}[0-9]{4}[A-Z]{1}';

    }

     

    public void PanCheck()

    {
      
        panCardList.add(applicant.CustBusinessPro__PAN__c);
        System.debug('----------------------' + panCardList);
        for(String panCard : panCardList)
        {
            pattern MyPattern = pattern.compile(PanFormat);
            matcher MyMatcher = MyPattern.matcher(panCard);

            if(MyMatcher.matches())

            {

                System.debug(panCardList + '---------------Valid');

            }

            else

            {

                System.debug(panCardList + '---------------Invalid');

            }

        }

    }

}User-added imageUser-added imageUser-added imageUser-added image

Regards,
Revati
BALAJI CHBALAJI CH
Hi Revati,

After seeing your screenshots I understood that my previous suggestion was wrong, sorry about that.
But I suppose that the code which you have pasted lastly should definitely work. What error are you getting when you try that code ?
 
public with sharing class PAN_Check
{
    public CustBusinessPro__Applicant__c applicant{get;set;}  
    List <String> panCardList{get;set;}   
    String PanFormat{get;set;}   
    
    public PAN_Check()      
    {    
        applicant = new CustBusinessPro__Applicant__c();   
        panCardList = new List<String> ();        
        PanFormat = '[A-Z]{5}[0-9]{4}[A-Z]{1}';        
    }    
        
    public void PanCheck()        
    {       
        panCardList.add(applicant.CustBusinessPro__PAN__c);
        System.debug('----------------------' + panCardList);
        for(String panCard : panCardList)
        {
            pattern MyPattern = pattern.compile(PanFormat);
            matcher MyMatcher = MyPattern.matcher(panCard);
            
            if(MyMatcher.matches())  
            {
                System.debug(panCardList + '---------------Valid');                
            }
            
            else                
            {                
                System.debug(panCardList + '---------------Invalid');
            }
        }
        
    }
}

Best Regards,
BALAJI

 
Revati BhavsarRevati Bhavsar
Hi Balaji CH,
      After executing the code in the developer console nothing is getting printed in debug logs.And when I execute "Execute Highlighted" I get an error saying variable "applicant.CustBusinessPro__PAN__c" not found on following line:
panCardList.add(applicant.CustBusinessPro__PAN__c);
System.debug('----------------------' + panCardList);

Which means I am not able to fetch the contents of CustBusinessPro__PAN__c.This class is called after upserting  records successfully.Here is the code of the Applicant Controller class which has PageReference of the Save button:

public with sharing class ApplicantController {
public CustBusinessPro__Applicant__c applicant{get;set;}
PAN_Check p = new PAN_Check();
................
................ 
 public PageReference save() {

    upsert applicant;
    p.PanCheck();
   return new PageReference('/' + applicant.Id);
    }
}

Regards,
Revati