Как проанализировать строки на Applescript/mac

Как я записал бы сценарий для анализа строк в Applescript (или другая программа сценариев для Mac)? Пример: просканируйте текстовый файл для, "Я люблю есть", копируют последующий текст, конец в".", так, чтобы, если мой файл был, "Я любил есть яблоки. Я люблю есть вишни. Я люблю есть виноград, орехи пекан и персики". это возвратило бы "виноград вишен яблок, орехи пекан и персики"

0
задан 15.01.2011, 14:05

1 ответ

Обычный подозреваемый для обработки текста в AppleScript является встроенным, глобальным свойством text item delimiters. Можно использовать его для оба, повреждают единственную строку в несколько частей (при использовании text items … of ссылочная форма), выберите конечные точки диапазона (при использовании text item N в text … of ссылочная форма), и соединение несколько частей в единственную строку (при принуждении списка к строке).

to pickText(str, startAfter, stopBefore)
    set pickedText to {}
    set minLength to (length of startAfter) + (length of stopBefore)
    repeat while length of str is greater than or equal to minLength
        -- look for the start marker
        set text item delimiters to startAfter
        -- finish if it is not there
        if (count of text items of str) is less than 2 then exit repeat
        -- drop everything through the end of the first start marker
        set str to text from text item 2 to end of str

        -- look for the end marker
        set text item delimiters to stopBefore
        -- finish if it is not there
        if (count of text items of str) is less than 2 then exit repeat
        -- save the text up to the end marker
        set end of pickedText to text item 1 of str

        -- try again with what is left after the first end marker
        set str to text from text item 2 to end of str
    end repeat
    set text item delimiters to " "
    pickedText as string
end pickText

-- process some “hard coded” text
set s to "I love eating apples. I love eating cherries. I love eating grapes, pecans, and peaches."
pickText(s, "I love eating ", ".") --> "apples cherries grapes, pecans, and peaches"


-- process text from a file
set s to read file ((path to desktop folder as string) & "Untitled.txt")
pickText(s, "I love eating ", ".")
1
ответ дан 24.11.2019, 12:12

Теги

Похожие вопросы