Find/Replace exact match, whole words

Hi,

I’m looking for a find/replace script that looks for whole words, so that a search for “Section 1” doesn’t return the “Section 1” part of “Section 10”, “Section 11”, and so on. Is this possible with applescript, or should I consider a shell script with regular expressions? Any help would be appreciated.

Thanks,
RJames

This is a first cut at it, but notice that it might have unintended consequences (see the last phrase).


set T to "This text has Section 1, Section 11, and Section 15 in it. I'd like to replace 11 with 12 if it follows the word Section. Section 12 refers to Blah, but Section 11 is about Bling."

set sTerm to "11"
set nTerm to "12"
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to sTerm
set p to text items of T
repeat with aString in p
	if last word of aString is "Section" then set contents of aString to aString & "*_*"
end repeat
set T to p as string
set AppleScript's text item delimiters to "*_*" & sTerm
set p2 to text items of T
set AppleScript's text item delimiters to nTerm
set T to p2 as string
set AppleScript's text item delimiters to tid
--> "This text has Section 1, Section 12, and Section 15 in it. I'd like to replace 11 with 12 if it follows the word Section. Section 12 refers to Blah, but Section 12 is about Bling.

Actually, what I want to do in the above example is replace every instance of “Section 1” with something like “Section One” without affecting “Section 11”, “Section 12”, “Section 101” and so on. That is, I don’t want “Section 101” to become “Section One01”.

Sorry if I was unclear before.

Thanks,
R.

There are undoubtedly more rules, so let’s be clear. Is it only the number 1 that is to be replaced with the word “One” if and only if it follows the word Section?

Try something like this:


to |switch word text| at t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	repeat with i from 2 to count t
		tell t's item i to if (count words) is 0 or word 1 does not start with character 1 then
			set p to i - 1
			set t's item p to t's items p thru i as text
			set t's item i to false
		end if
	end repeat
	set text item delimiters to s
	set t to t's text as text
	set text item delimiters to d
	t
end |switch word text|

set t to "This text contains Section 1, Section 11, Section 12 and Section 101. We need to replace occurrences of Section 1, but not of Section 11, Section 12 or Section 101."

|switch word text| at t from "Section 1" to "Section One" --> "This text contains Section One, Section 11, Section 12 and Section 101. We need to replace occurrences of Section One, but not of Section 11, Section 12 or Section 101."


Ahh, the key! :slight_smile:

tell t's item i to if (count words) is 0 or word 1 does not start with character 1 then

Other than that, I had set t’s item i to missing value to get rid of it - didn’t know false worked :rolleyes:
Great to see your solution - I fiddled for a while before having to drop it.

Adam