I have the following function:
@testVisible private String selectBeforeKeyword (String remainingStepsAsString, String keyword)
{
List<String> splitList = remainingStepsAsString.split('\\b'+keyword+'\\b');
return (splitList.size() > 1)
? (splitList[0].trim())
: null;
}
This works as I need it to. Yet I am not really looking to split the the string, but rather to capture the beginning of it. So, I'm thinking a more appropriate solution might be something like this:
@testVisible private String selectBeforeKeyword (String remainingStepsAsString, String keyword)
{
Pattern keywordPattern = Pattern.compile('(.*)\\b'+keyword+'\\b');
Matcher keywordMatcher = keywordPattern.matcher(remainingStepsAsString);
return (keywordMatcher.matches())
? (keywordMatcher.group(0).trim())
: null;
}
... however, this doesn't work, returning null instead.
What would the correct pattern to compile to make this work?