1

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

2 Answers 2

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

if(isMatched){
//goes here }
  • is shifted to correct place
8
  • 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. Commented Mar 11, 2014 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-True');
    – highfive
    Commented Mar 11, 2014 at 9:18
  • Have u tried the above? its returning false when I am passing this value: abc-Insert-def-True Commented Mar 11, 2014 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
    Commented Mar 11, 2014 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); Commented Mar 11, 2014 at 9:43
2

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
2
  • No, something more generic I am looking for. Commented Mar 11, 2014 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. Commented Mar 11, 2014 at 8:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .