Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I need to check a string using apex whether it is in the following format:

  1. Anything-Insert-Anything-True
  2. Anything-Update-Anything-True
  3. Anything-Delete-Anything-False

Anything(it can be any string).
The last one may be true or false.

How to check it using apex? need help

share|improve this question

2 Answers 2

up vote 1 down vote accepted
Boolean isMatched =
Pattern.matches('(.*-Insert-.*-)(True|False)','abc-Insert-def-True');
System.debug('Result : '+isMatched);

if(isMatched){
//goes here }
  • is shifted to correct place
share|improve this answer
    
Thanks a lot for the response.it is quite close. Just one thing, i can have insert or update or delete.For that what i need to do. –  LoveSalesforce Mar 11 at 9:15
    
Hey, it's another one. I just answered for what you have asked ;) Try this Pattern.matches('(.-*(Insert|Update|Delete)-.*-)(True|False)','abc-Insert-def-T‌​rue'); –  highfive Mar 11 at 9:18
    
Have u tried the above? its returning false when I am passing this value: abc-Insert-def-True –  LoveSalesforce Mar 11 at 9:35
    
Check your matcher. It's having '-' at the end which will not match with the pattern. The only difference of my two answers is second one accepting Insert or Update or Delete –  highfive Mar 11 at 9:38
    
When i run this in console-> Boolean isMatched = Pattern.matches('(.-Insert-.-)(True|False)','abc-Insert-def-True'); It returns false. please check System.debug('Result : '+isMatched); –  LoveSalesforce Mar 11 at 9:43

You can use the apex string methods as following:

string s = 'Anything-Insert-Anything-True';

if(s.contains('-Insert-') && s.endsWith('-True')){
        system.debug('success');
}
//repeat for your other possibilities
share|improve this answer
    
No, something more generic I am looking for. –  LoveSalesforce Mar 11 at 8:40
    
Hmm, could you then reflect that and any other conditions you have in your question by an update ? Else it's pretty hard for anyone to answer you an answer you're really looking for. –  Samuel De Rycke Mar 11 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.