Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 down vote accepted
Boolean isMatched =
System.debug('Result : '+isMatched);

//goes here }
  • is shifted to correct place
share|improve this answer
Thanks a lot for the is quite close. Just one thing, i can have insert or update or delete.For that what i need to do. – LoveSalesforce Mar 11 '14 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 '14 at 9:18
Have u tried the above? its returning false when I am passing this value: abc-Insert-def-True – LoveSalesforce Mar 11 '14 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 '14 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 '14 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')){
//repeat for your other possibilities
share|improve this answer
No, something more generic I am looking for. – LoveSalesforce Mar 11 '14 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 '14 at 8:43

Your Answer


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.