12 Replies Latest reply on May 16, 2017 3:07 PM by boilinh2o

    Parsing Email addresses out of a string

    boilinh2o

      Hello,

       

      I have a field that is populated by a mail archive - call it recieverTO.

       

      The content can look like this:

       

      Nathan Smith nathan@domain.com,Mark Jones mark@otherdomain.com

       

      or this

      Nathan Smith nathan@domain.com

       

      or this

      Nathan Smith nathan@domain.com,mark@otherdomain.com

       

      and maybe even this

      Nathan Smith nathan@domain.com,mark@otherdomain.com, Terry Brown Terry@browndomain.com

       

      I want to grab out the email addresses. Preferably all of them, but failing that at least the first one.

       

      I'm guessing that I might need to script this. Maybe looping through and grabbing the first and assigning it to a variable and then the next... Once I have them in variables I can stick them somewhere where I can use them.

       

      Any suggestions is appreciated.

       

      Mark

        • 1. Re: Parsing Email addresses out of a string
          philmodjunk

          Do you want the names, or just the Email addresses?

           

          Assuming just the email addresses,

           

          Set Variable [ $EmailList ; value; Substitute ( Text ; "," ; ¶ ) ]

           

          will give you a return separated list of values with one address in each value of the list. GetValue can be used to extract one value at a time from the list and you can remove the names by referencing all text to the right of the last space character.

           

          Let ( [ Element = GetValue ( $EmailList ; $K ) ; // $K is loop counter

                    start = Position ( Element ; " " ; -1 ; Length ( Element ) ) ]; // position is set to search right to left

                   Middle ( Element ; Position + 1 ; Length ( Element ) )

                ) // Let

          1 of 1 people found this helpful
          • 2. Re: Parsing Email addresses out of a string
            boilinh2o

            Hello philmodjunk,

             

            Thanks for the info. I could not get it to work, but it lead me to RTFM with a focus on the Let and GetValue.

             

            I came up with a calc that I use in setting field content.

             

            The one thing that I could figure out was how to set the value for $K in your suggestion.  I couldn't find a way to "see" how many values where within a variable or field so that I could work out the loop.

             

            In the particular context that I need to parce email addresses there are rarely more then 5 addresses in a TO or CC field so I could just work through 5 levels of values:

             

            Let ( $Element = GetValue ( $EmailList ; 1 ) ;

             

             

                  If(PatternCount ( $Element ; " " )=0;

             

             

                   $Element

             

             

                  ;

                     If(PatternCount ( $Element ; " " )=1;

             

             

                       Let ( SpacePosition = Position ($Element ; " " ;1;1);

             

             

                             Right ( $Element ; Length ( $Element ) - SpacePosition )

                            )

                 ;

             

             

                     If(PatternCount ( $Element ; " " )=2;

             

             

                        Let ( SpacePosition = Position ($Element ; " " ;1;2);

             

                        Right ( $Element ; Length ( $Element ) - SpacePosition )

                       )

             

             

                  ;

                     If(PatternCount ( $Element ; " " )=3;

             

             

                         Let ( SpacePosition = Position ($Element ; " " ;1;3);

             

             

                             Right ( $Element ; Length ( $Element ) - SpacePosition )

                            )

             

             

                    ;$Element))))

             

             

             

             

            ) &

             

             

             

             

            If($Element = GetValue ( $EmailList ; 2 ) ≠  "";

              

                "¶" &

             

             

                Let ( $Element2 = GetValue ( $EmailList ; 2 ) ;

             

                    If(PatternCount ( $Element2 ; " " )=0;

             

             

                         $Element2

                     ;

                       If(PatternCount ( $Element2 ; " " )=1;

             

             

                           Let ( SpacePosition = Position ($Element2 ; " " ;1;1);

             

             

                             Right ( $Element2 ; Length ( $Element2 ) - SpacePosition )

                            )

                      ;

                        If(PatternCount ( $Element2 ; " " )=2;

             

             

                           Let ( SpacePosition = Position ($Element2 ; " " ;1;2);

             

             

                               Right ( $Element2 ; Length ( $Element2 ) - SpacePosition )

                               )

             

             

                      ;

                       If(PatternCount ( $Element2 ; " " )=3;

             

             

                           Let ( SpacePosition = Position ($Element2 ; " " ;1;3);

             

             

                             Right ( $Element2 ; Length ( $Element2 ) - SpacePosition )

                            )

                    ;"")))))

             

             

            )

             

             

            &

             

             

             

             

            If($Element = GetValue ( $EmailList ; 3 ) ≠  "";

              

                "¶" &

             

             

                Let ( $Element3 = GetValue ( $EmailList ; 3 ) ;

             

            If(PatternCount ( $Element3 ; " " )=0;

             

             

                         $Element3

                     ;

                       If(PatternCount ( $Element3 ; " " )=1;

             

             

                           Let ( SpacePosition = Position ($Element3 ; " " ;1;1);

             

             

                             Right ( $Element3 ; Length ( $Element3 ) - SpacePosition )

                            )

                      ;

                        If(PatternCount ( $Element3 ; " " )=2;

             

             

                           Let ( SpacePosition = Position ($Element3 ; " " ;1;2);

             

             

                               Right ( $Element3 ; Length ( $Element3 ) - SpacePosition )

                               )

             

             

                      ;

                       If(PatternCount ( $Element3 ; " " )=3;

             

             

                           Let ( SpacePosition = Position ($Element3 ; " " ;1;3);

             

             

                             Right ( $Element3 ; Length ( $Element3 ) - SpacePosition )

                            )

                    ;"")))))

             

             

             

             

            )

             

             

            &

             

             

             

             

            If($Element = GetValue ( $EmailList ; 4 ) ≠  "";

              

                "¶" &

             

             

                Let ( $Element4 = GetValue ( $EmailList ; 4 ) ;

             

                    If(PatternCount ( $Element4 ; " " )=0;

             

             

                         $Element4

                     ;

                       If(PatternCount ( $Element4 ; " " )=1;

             

             

                           Let ( SpacePosition = Position ($Element4 ; " " ;1;1);

             

             

                             Right ( $Element4 ; Length ( $Element4 ) - SpacePosition )

                            )

                      ;

                        If(PatternCount ( $Element4 ; " " )=2;

             

             

                           Let ( SpacePosition = Position ($Element4 ; " " ;1;2);

             

             

                               Right ( $Element4 ; Length ( $Element4 ) - SpacePosition )

                               )

             

             

                      ;

                       If(PatternCount ( $Element4 ; " " )=3;

             

             

                           Let ( SpacePosition = Position ($Element4 ; " " ;1;3);

             

             

                             Right ( $Element4 ; Length ( $Element4 ) - SpacePosition )

                            )

                    ;"NO4")))))

             

             

            )

             

             

            &

             

             

             

             

            If($Element = GetValue ( $EmailList ; 5 ) ≠  "";

              

                "¶" &

             

             

                Let ( $Element5 = GetValue ( $EmailList ; 5 ) ;

             

                    If(PatternCount ( $Element5 ; " " )=0;

             

             

                         $Element5

                     ;

                       If(PatternCount ( $Element5 ; " " )=1;

             

             

                           Let ( SpacePosition = Position ($Element5 ; " " ;1;1);

             

             

                             Right ( $Element5 ; Length ( $Element5 ) - SpacePosition )

                            )

                      ;

                        If(PatternCount ( $Element5 ; " " )=2;

             

             

                           Let ( SpacePosition = Position ($Element5 ; " " ;1;2);

             

             

                               Right ( $Element5 ; Length ( $Element5 ) - SpacePosition )

                               )

             

             

                      ;

                       If(PatternCount ( $Element5 ; " " )=3;

             

             

                           Let ( SpacePosition = Position ($Element5 ; " " ;1;3);

             

             

                             Right ( $Element5 ; Length ( $Element5 ) - SpacePosition )

                            )

                    ;"NO5")))))

             

             

            )

            • 3. Re: Parsing Email addresses out of a string
              philmodjunk

              $K is a loop variable.

               

              Loop

                  Set variable [[ $K ; value: $K + 1 ]

                  Exit Loop If [ $K > ValueCount ( $EmailList ) ]

                   Put original calculation here

              EndLoop

              1 of 1 people found this helpful
              • 4. Re: Parsing Email addresses out of a string
                boilinh2o

                Hot Damn.. I was searching around for a tool: ValueCount. That will make things far more elegant and open to huge strings of email addresses...

                 

                Thanks for the fishing lesson!

                • 5. Re: Parsing Email addresses out of a string
                  boilinh2o

                  Here's the final script:

                   

                   

                  Set Variable [ $EmailList; Value:Substitute ( Mail::ReceiverTo ; "," ; "¶" ) ] Set Field [ Mail::ReceiverTo_parsed; $EmailList ]
                  Set Field [ Mail::EmailParse_ValueCount; ValueCount ($EmailList) ] Commit Records/Requests

                  Loop
                  Set Variable
                  [ $k; Value:$k +1 ]
                  Exit Loop If [ $k > ValueCount ( $EmailList ) ]
                  Set Variable [ $EmailAddress[$K]; Value:Let ( $Element = GetValue ( $EmailList ; $K );

                  If(PatternCount ( $Element ; " " )=0; $Element

                  ;
                  If(PatternCount ( $Element ; " " )=1;

                  Let ( SpacePosition = Position ($Element ; " " ;1;1);

                  Right ( $Element ; Length ( $Element ) - SpacePosition ) )

                  ;
                  If(PatternCount ( $Element ; " " )=2;

                  Let ( SpacePosition = Position ($Element ; " " ;1;2);

                  Right ( $Element ; Length ( $Element ) - SpacePosition ) )

                  ;
                  If(PatternCount ( $Element ; " " )=3;

                  Let ( SpacePosition = Position ($Element ; " " ;1;3);

                  Right ( $Element ; Length ( $Element ) - SpacePosition ) )

                  ;$Element))))) ]
                  Insert Calculated Result [ Mail::ReceiverTo_parsed2; If($K=1;$EmailAddress[$K];"¶" & $EmailAddress[$K]) ]

                  End Loop

                   

                  • 6. Re: Parsing Email addresses out of a string
                    erolst

                    For the penultimate step, you may be interested in the List() function that simplifies adding values to a list by adding a CR only as necessary, like

                     

                    Set Field [ Mail::ReceiverTo_parsed2 ; List ( Mail::ReceiverTo_parsed2 ; $EmailAddress[$k] ) ]

                     

                    while Set Field[] does not require the target field on the layout.

                     

                    Speaking of simplifying: you can consolidate the variable calculation parsing like this:

                     

                    Set Variable [ $EmailAddress [$k] ;

                     

                      Let ( [

                        element = GetValue ( $EmailList ; $k ) ;

                        spaceCount = PatternCount ( element ; " " ) ;

                        spacePosition = Position ( element ; " " ; 1 ; spaceCount )

                        ] ;

                        If (

                          not spaceCount ;

                          element ;

                          Right ( element ; Length ( element ) - spacePosition )

                        )

                      )

                     

                    ]

                     

                    Note that a Let() allows the creation of specific variables (without a $) that are only valid within that Let()'s scope.

                    1 of 1 people found this helpful
                    • 7. Re: Parsing Email addresses out of a string
                      philmodjunk

                      Don't use Insert Calculated Result for this script. Scripts that start with the word "insert" are inherently "brittle", fairly mundane layout changes can cause the script to fail in the future. Instead, use Set Field as it only requires the correct table occurrence context as determined by the current layout to work correctly.

                       

                      "insert" steps:

                       

                      Don't work for a specified field if that field is not present on the current layout. Remove that field in a later layout update and the script fails.

                       

                      Puts the focus in the specified field. This can trip a number of different script triggers. Simply adding a trigger to a field at some point in the future may be enough to cause problems if the trigger performed script interferes with the desired results.

                      1 of 1 people found this helpful
                      • 8. Re: Parsing Email addresses out of a string
                        boilinh2o

                        Hi Erolst,

                         

                        Thanks for the additional insight. My head's wrapping around it. I forgot that Insert Calc requires the field to be on the layout, when I finally get around to making the UX Interface, It' would be cool to not see that.

                         

                        I see the simplicity in compressing the logic down with the Let statement.

                         

                        I'm not familiar with the calc: not X

                         

                        I'm assuming that it returns affirmative if X is null.

                        • 9. Re: Parsing Email addresses out of a string
                          erolst

                          Yes, 'not 0' means Boolean True.

                           

                          Whenever you need a Boolean value (If[], If(), Case(), Conditional Formatting, Hide if etc. etc.) and you have a number, 0 means False, everything else means True, and not lets you negate/reverse that.

                           

                          Therefore its more convenient to write

                           

                          If [ not Get ( FoundCount ) ]

                           

                          to start a block with code to be executed if the current found set is empty (has 0 records).

                           

                          Note that the more verbose

                           

                          x = 0 / If [ Get ( FoundCount ) = 0 ]

                           

                          is a perfectly valid expression, so by all means use it if you feel more comfortable.

                          • 10. Re: Parsing Email addresses out of a string
                            boilinh2o

                            so in the context of your Let statement above

                             

                            not spaceCount  = spaceCount="" and then in the If statement above the True result would be executed.

                             

                             

                            • 11. Re: Parsing Email addresses out of a string
                              erolst

                              Not exactly; spaceCount cannot be empty because it contains the result of PatternCount(), and that will be either 0 (no pattern match) or another number.

                               

                              So, the expression 'not spaceCount' is equal to the expression 'spaceCount = 0'.

                               

                              Note that this only works reliably for numeric values.

                               

                              If that sort of shorthand confuses you at the moment, just be more verbose in your code.

                              • 12. Re: Parsing Email addresses out of a string
                                boilinh2o

                                I get it.

                                 

                                And then I'll use it in another context and find out if I really do.