Extract 5 characters from a string with Offset

I need to extract five text characters from a string of text. Example I have the string “My Report is 02507 of the set of reports”. I want to extract the string “02507”. The characters “02” will always be unique and the next three characters will vary.

I can use Offset to locate the “02” characters, but how do I extract all five characters as a variable that I can use later in my script?

A possible way is Regular Expression and AppleScriptObjC


use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theString to "My Report is 02507 of the set of reports"
set cocoaString to current application's NSString's stringWithString:theString
set theRange to cocoaString's rangeOfString:"02\\d{3}" options:(current application's NSRegularExpressionSearch)
if theRange's location() as integer < (count theString) then
	set extractedString to (cocoaString's substringWithRange:theRange) as text
else
	set extractedString to "Not found"
end if
display dialog extractedString buttons {"Cancel", "OK"} default button "OK"

The pattern “02\d{3}” searches for 02 and three digits.

You may use old fashioned code:

set theString to "My Report is 02507 of the set of reports"
set off7 to offset of "02" in theString
set extractedString to text off7 thru (off7 + 5) of theString

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 18 aout 2020 19:40:57

Here are three different methods.

--Method 1:  Done in two lines
set theString to "My Report is 02507 of the set of reports"
set foundString to characters (offset of "02" in theString) thru ((offset of "02" in theString) + 4) of theString as string

--Method 2:  Conceptually Simpler
set theString to "My Report is 02507 of the set of reports"
set theLoc to offset of "02" in theString
set foundString to characters theLoc thru (theLoc + 4) of theString as string

--Method 3: As Subroutine for future flexibility
set theString to "My Report is 02507 of the set of reports"
set foundString to extractString(theString, "02")

on extractString(theString as string, searchFor as string)
	set theLoc to offset of searchFor in theString
	return characters theLoc thru (theLoc + 4) of theString as string
end extractString

Browser: Firefox 79.0
Operating System: macOS 10.14

FWIW, the “AppleScript Language Guide” recommends the use of “text” rather than “characters” in instances such as this:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-DontLinkElementID_617


--Method 1:  Done in two lines
set theString to "My Report is 02507 of the set of reports"
set foundString to text (offset of "02" in theString) thru ((offset of "02" in theString) + 4) of theString

--Method 2:  Conceptually Simpler
set theString to "My Report is 02507 of the set of reports"
set theLoc to offset of "02" in theString
set foundString to text theLoc thru (theLoc + 4) of theString

--Method 3: As Subroutine for future flexibility
set theString to "My Report is 02507 of the set of reports"
set foundString to extractString(theString, "02")

on extractString(theString as text, searchFor as text)
	set theLoc to offset of searchFor in theString
	return text theLoc thru (theLoc + 4) of theString
end extractString

Thank you for all the suggestions. These worked perfectly.

If the string always begin with My Report is then you could ask for word 4

set theString to "My Report is 02507 of the set of reports"
its word 4 of theString as text

if you use

it will remove the first digital 0 and give 2507

I test your code StefanK and got some error if I change the regular expression or the text string.

but this worked…

if theRange's location() as text < (count theString) then
		set extractedString to (cocoaString's substringWithRange:theRange) as text
	else
		set extractedString to "Not found"
	end if