You did not respond to my question about the format of that 34 character string in an email body. I have revised the original script that I posted by adding a regular expression pattern for that 34-character string, and made the AppleScript handler generic so that passing the email body text variable and the regular expression pattern will return the captured content (if found).
AppleScript
(*
Search a selected email body for a financial value, and a 34-character
string. Captured content is returned to their resepective variables.
Coded to handle $,£, and € currencies. Text string is case-insensitive
alphanumeric and may contain known punctuation characters. It should
be surrounded by white space.
Includes Unicode ICU Regular Expressions
https://unicode-org.githubhttps://unicode-org.github.io/icu/userguide/strings/regexp.html
Tested: Ventura 13.2, Apple Mail 16.0
VikingOSX, 2023-02-06, Apple Support Communities, No warranty.
*)
use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions
property ca : current application
-- ICU Regular Expression to match numbers up to 999p999p999p99
-- where p is the currency punctuation of either comma or dot
property ICU_Monetary : "([$£€]?\\d{1,3}(?:[.,]?\\d{3})*(?:[.,]?\\d{2}))"
-- get 34 contiguous case-insensitive characters surrounded by white-space
-- (e.g. Abu_dec-12xy_4A23BmDEFgnxyffz-ce5n )
-- uses positive lookbehind and lookahead for surrounding word boundaries
property ICU_Text34 : "(?<=\\b)([[:alnum:][:punct:]]{34})(?=\\b)"
tell application "Mail"
if not it is running then activate
if not (get selection) is {} then
set theMsg to item 1 of (get selection)
else
return
end if
tell theMsg
set theBody to its content
end tell
end tell
set money to my string_extraction(theBody, ICU_Monetary)
set text34 to my string_extraction(theBody, ICU_Text34)
log (money) as text
log (text34) as text
-- note only one item at a time may exist on the clipboard
if not money is equal to "Not Found" then
ignoring numeric strings
set the clipboard to money
end ignoring
end if
return
on string_extraction(atxt, regex)
set hstr to ca's NSString's alloc()'s initWithString:atxt
set regex to ca's NSRegularExpression's regularExpressionWithPattern:regex options:0 |error|:0
set hrange to ca's NSMakeRange(0, hstr's |length|())
set matches to (regex's firstMatchInString:hstr options:0 range:hrange)
if matches = "" or matches = missing value then return "Not Found"
set matchrange to matches's rangeAtIndex:1
return (hstr's substringWithRange:matchrange) as text
end string_extraction