applescript - 有没有办法从返回的文本中删除某些单词?
问题描述
这里的菜鸟编码器,
我正在尝试创建一个程序,该程序将从用户返回的句子中删除某些单词。
这是我到目前为止得到的:
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 没有正则表达式,但您可以使用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
推荐阅读
- android - 在 Wordpress 上添加新帖子时将 WordPress 的通知推送到 Android 应用程序?
- python - 将混合数据转换为字符串 numpy
- mongodb - Heroku mlab 附加组件在迁移到 mongoDB 地图集后被删除
- javascript - 使用 setState() 修改一个 JavaScript 数组会更新第二个状态数组
- flutter - Flutter / android如何计算相对于地面的加速度?
- spring - Springboot @Async 在多服务器环境中
- sql-server - 在 Docker 上使用 pyodbc 配置 SQLAlchemy - 出现接口错误
- java - Android/Java:如何导入 javax.media.j3d?
- python - 通过使用正则表达式选择列来更改 Pandas 列的数据类型
- sql - 如何从具有执行类型作为嵌入式查询的 Control-M 数据库作业运行 SQL Developer 中存在的存储过程