Extracting text between two different strings

Much as I’ve tried to find this precise answer, I seem to have found every other scenario but the precise one that I need.

Let’s say I have a long string (origString) and I want to find some specific text in it between two other strings (firstDelimString and lastDelimString).

I think the solution is related to delimiters – but every time I try one of the many examples I’ve gotten bizarre things. Some specific info that may be the key here to why.

  • origString is long and multiple lines (i.e., has return characters). Assume this is something like a page full of text.

  • firstDelimString and lastDelimString also are multiple lines – but will be shorter. Figure a few lines just to make sure they are unique. first will be before last.

  • we can assume one instance of the find – or at least that the first one is sufficient.

I’m hoping to have a subroutine that is basically this:

 on ExtractStringBetweenTwoOtherStrings (origString, firstDelimString, lastDelimString)
    -- some string magic based on the parameters passed
    return foundString
 end ExtractStringBetweenTwoOtherStrings

Thoughts?
Thanks!

Neil

Just a bit more info… I think the issue is the return characters. For example, in this code:


set theText to ExtractStringBetweenTwoOtherStrings(testString, startDelimiter, endDelimiter)
display dialog theText

on ExtractStringBetweenTwoOtherStrings(origString, firstDelimString, lastDelimString)
	set savedDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to firstDelimString
	set foundString to item 2 of every text item of origString
	set AppleScript's text item delimiters to lastDelimString
	set foundString to item 1 of every text item of foundString
	set AppleScript's text item delimiters to savedDelimiters
	
	return foundString
end ExtractStringBetweenTwoOtherStrings

if endDelimiter has no returns in it – it works. But, if it does have returns (e.g., is multiple lines), it does not work. In fact, it’s as if the second “extract” doesn’t happen at all.

In other words, if this is the case:


property endDelimiter : "Booth Location(s):"

it works. But, if this is the case:


property endDelimiter : "
Booth Location(s):"

it does not work … even though the endDelimiter does have a blank link behind it in the original text.

Ideas?

Thanks!

Neil

Hi. You haven’t defined your test string in this question, and I can’t tell whether or not you are trying to include the return, which may actually be a linefeed. I simplified this for you, using two delimiters.

set teststring to "Let's say I have a long
string (origString) and I want to find some specific text in it between two other strings (firstDelimString and lastDelimString).

I think the solution is related to delimiters -- but every time I try one of the many examples I've gotten bizarre things.  
Some specific info that may be the key here to why."


------------------------------------------------------------------------------------------------------------------------
set AppleScript's text item delimiters to {"Let's say I have a long" & linefeed, "key here to why."} --& linefeed, if you want to exclude it
teststring's text item 2

Yep … I think that’s the ticket! Thanks!

The code I posted was good, but when I define the other text, I needed to make sure it was seeing it as a return instead of a linefeed. For example:


property startDelimiter : "logo goes here" & return & "MENU " & return
property endDelimiter : return & "Location(s):"

instead of:


property startDelimiter : "logo goes here
MENU 
"
property endDelimiter : "
Location(s):"

Once I did that, it worked perfectly. With that in mind, is there an easy way to add in another line that would automatically convert any linefeed in any of the parameters (origString, firstDelimString, lastDelimString) to a return?

Thanks!

Neil

Hi, Neil. This would convert:


--enforce returns
set text item delimiters to {return, linefeed} --destroy
set teststring to teststring's text items as text --reassemble using 1st delim

--get betwixt
set text item delimiters to {"Let's say I have a long" & return, "key here to why."}
teststring's text item 2