首页 > 解决方案 > 有没有办法从返回的文本中删除某些单词?

问题描述

这里的菜鸟编码器,

我正在尝试创建一个程序,该程序将从用户返回的句子中删除某些单词。

这是我到目前为止得到的:

    set returnedSentence to display dialog "Welcome to SimpleScript Early Release (Beta)! To continue please enter your desired code/text." default answer "" buttons {"Go!", "Cancel"} default button 1

display dialog returnedSentance - "if" - "and" - "then" - "a" buttons {"ok", "Cancel"} default button 2

例如:如果用户输入句子:如果我饿了,那么我会去餐馆。

然后代码应该返回我饿了,我会去餐厅。

提前致谢。

标签: applescript

解决方案


普通 AppleScript 没有正则表达式,但您可以使用text item delimiters空/空白替换所需的文本项。例如:

set returnedSentence to text returned of (display dialog "Welcome to SimpleScript Early Release (Beta)! To continue please enter your desired code/text." default answer "" buttons {"Go!", "Cancel"} default button 1)

set trimList to {"if ", "and ", "then ", "a "} -- the text items to replace (note the trailing space to denote words)
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, trimList} -- stash original delimiters (normally blank) and set new ones
set trimmedItems to text items of returnedSentence -- break the string apart
set AppleScript's text item delimiters to tempTID -- restore delimiters (normally blank)
set trimmedSentence to trimmedItems as text -- put the string back together

display dialog trimmedSentence buttons {"ok", "Cancel"} default button 2

推荐阅读