0

This is racking my brain! In apex, I need to determine whether or not a specific sub-string is in a picklists value. So I'm trying to use the Matcherclass and the find() method using regex. My problem is that I can't figure out the regex I need. So, here is an example :

Need to determine if a string like 'Sales Manager' is in : 'Associate;Assistant Sales Manager;Regional Sales Manager;HR Manager'.

The result for the list above would be false.

But true here:

'Sales Manager' is in : 'Associate;Assistant Sales Manager;Sales Manager;Regional Sales Manager;HR Manager'

I tried \\\bSales Manager\\\b but that's not it. Sales manager is normally a variable that I need to search. Any and all help is greatly appreciated!!

3
  • You can use this also - contains(substring)...Which returns true if specified String found in the string.
    – MIX DML
    Commented Oct 21, 2015 at 13:49
  • He would have to be careful with that as it would return true because "Sales Manager" is a substring of "Assistant Sales Manager" as well.
    – smukov
    Commented Oct 21, 2015 at 13:53
  • Yeah contains doesn't work, due to the similar values. That is what I had first and noticed other types were getting flagged as true! :P
    – KidNiki
    Commented Oct 21, 2015 at 13:56

2 Answers 2

0

Thank you everyone for the ideas, I ended up using a the split method with a regex of .split('[;]{1}'); and then iterated through the array looking for a match!

0

How about this - (^Sales Manager)|(;Sales Manager)

Try it here (http://regexr.com/3c1hs) against this string: Sales Manager;Associate;Assistant Sales Manager;Sales Manager;Regional Sales Manager;HR Manager;Sales Manager

It finds the Sales Manager if it's the first in the string, last or in the middle.

Usage sample:

Pattern myPattern = Pattern.compile('(^Sales Manager)|(;Sales Manager)');

Matcher myMatcher = myPattern.matcher('Associate;Assistant Sales Manager;Sales Manager;Regional Sales Manager;HR Manager');

//myMatcher.find() returns TRUE if it finds a match
System.assert(myMatcher.find());
1
  • Awesome, I'll give that a shot and flag this if it works!
    – KidNiki
    Commented Oct 21, 2015 at 13:57

You must log in to answer this question.

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