regex - bash中变量的多个正则表达式替换?

标签 regex bash

我想知道是否有办法在 bash 中使用 multilple regexp 替换 ${string//substring/replacement} 或者,可能存在更好的解决方案。

我有一个脚本可以使用 curl 将更新发送到 statusnet 和 friendika。 例如,我在网上签署请愿书,并被邀请在推特上发布,但我宁愿发送到 identica。我厌倦了粘贴东西并且不得不在终端中编辑以逃避@和#,!和 ?。 我想在我的脚本中用正则表达式替换它们以自动更改

@repbunghole and @SenatorArsehat Stop farking around with #someship! Do you want me to come smack you? | http://someshort.url

\@repbunghole \@SenatorArsehat Stop farking around with \#someship\! Do you want me to come smack you\? \| http://someshort.url

我没有强大的 sed 或 awk fu,但想象他们可能会提供解决方案,而且我不知道如何在不将变量写入文件、读取文件并对其执行操作,然后设置 var 的情况下使用 sed使用 var=$(cat 文件)。是的。我在这方面很陌生。 我没有用上面的 ${string//substring/replacement/} 找到足够的数据来进行多次替换。运行 X 次以转义 X 个不同的字符似乎效率很低。

喜欢

read -p "Enter a string: " a
b=${a//\@/\\\@}
c=${b//\#/\\\#}
d=${c//\!/\\\!}
e=${d//\?/\\\?}
f=${e//\"/\\\"}
g=${f//\'/\\\'}

等等等等 同时工作,但它很丑......

最佳答案

这就是character classes用于:

b=${a//[@#!?"']/\\\0}

关于regex - bash中变量的多个正则表达式替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7585564/

相关文章:

string - sed 同时和顺序替换

linux - 如何将输出分配给两个 bash 数组变量

bash - 如何验证由Dockerfile执行的bash脚本生成的输出

asp.net - 重定向主页

php - MySql按月、按周的时间戳查询及比较

regex - 在 Sublime Text 2 中展开文本

Bash 将分隔文件中的所有整数减一

javascript - NPM 或任何其他包的名称和任何版本号的正则表达式

javascript - 我们如何为 ex_date(ddsf#dsd) 编写正则表达式?

bash - crontab 在 ffmpeg 中的转换过程结束之前再次启动(取决于时间假设/2 分钟)如何控制?