0

I've been searching through past questions and other literature to try to figure this out but it's time to just ask outright.

I'm trying to write an after insert trigger that takes the string value between the first encountered ( and ) in a text field, and passes it into a map for an ID return to be used in a new record creation.

I've experimented with patterns+matchers as well as indexOf code, without success. Could anyone assist with examples of either? Thanks.

Code I've begun (using the Event object):

for(Event e : trigger.new){
     String EDescription = Event.Description;
     String ExtractedString = EDescription.indexOf("(") + 1,   myString.lastIndexOf(")"));
}

et cetera...

I've also tried modifying the code found at Can anyone help with a formula to pull an email address from a text string in a standard field?

Namely:

for (Event e: Trigger.new) {
    if (e.Description != null) {
        Pattern StringPattern = Pattern.compile('(?i)^([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})$');
        for (String bit: e.Description.split(' ')) {
            Matcher StringMatcher = StringPattern.matcher(bit);
            if (StringMatcher.matches()) {
                e.Description_ExtractedString__c = bit;
            }
        }
    }
}

I've not yet modified the pattern-matcher RegEx code specifically enough but I believe the context is clear.

3
  • Could you post some of your code? Commented Mar 11, 2015 at 9:49
  • @mast0r Code posted. I'll look into cleaning up its presentation, but it is accurate so far. Commented Mar 11, 2015 at 10:14
  • Perhaps I should be looking at using substringBetween, rather than making it more complicated? Commented Mar 11, 2015 at 10:24

1 Answer 1

0

The code below will get the text from within the brackets. The code makes use of some String methods

String eDescription = 'This is (the test) text';
Integer openingIndex = eDescription.indexOf('(');
Integer closingIndex = eDescription.indexOf(')');
String textInBrackets = eDescription.subString(openingIndex + 1, closingIndex);

Using @Sovereigntys suggestion of using substringBetween then the code becomes:

String eDescription = 'This is (the test) text';
String textInBrackets = eDescription. substringBetween('(', ')');

FYI: Both ways will find the first instance of '(' and ')'.

  • Input: 'This is (the test) text', Output: 'the test'
  • Input: 'This is (the test) (text)', Output: 'the test'
5
  • Thanks for your reply @BarCotter. Would using substringBetween make it more efficient, or is the difference negligible? What would be the best practice method for snipping text out like this? Commented Mar 11, 2015 at 10:36
  • I believe I have this working now via substringBetween and other examples from elsewhere. Thanks everybody! Commented Mar 11, 2015 at 11:51
  • It makes the code shorter. I don't know if its more efficient, it may be actually doing the exact same thing behind the scenes
    – BarCotter
    Commented Mar 11, 2015 at 11:54
  • Do you think substringBetween would run into problems if the text field has more than one '(' and ')' in its value? Does substringBetween perform its function only on the first encountered characters from Left to Right? Commented Mar 11, 2015 at 11:57
  • The code won't throw any errors if there a multiple brackets in the code. It will always find the text before the first '(' and ')'
    – BarCotter
    Commented Mar 11, 2015 at 12:02

You must log in to answer this question.

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