regex - Sed 只识别部分搜索模式
问题描述
我正在寻找一种方法来用常量替换文件中所有出现的网站。我gsed
在我的 mac 上使用和正则表达式(不要偏离 mac 一词,因为这与我在 Windows 机器上执行时得到的输出相同)来完成此操作。我能够成功验证 regex101.com 上的正则表达式,但由于某种原因 sed 替换失败
gsed --version : gsed (GNU sed) 4.8
(g)sed
命令:
find . -type f -path "./file1.txt" -exec gsed -i -E -f /tmp/scripts/regex {} \;
/tmp/scripts/regex
内容:
s/(ftp|http[s]?):\/\/([\w\.-]+)/\1{Your_Site}/gI
样本file1.txt
内容:
* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://example.org\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://another-link-to-my-example.org/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n
预期输出:
* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://{Your_Site}\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://{Your_Site}/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n
电流输出:
* "{\n \"firstName\": \"\",\n \"lastName\": \"\",\n \"street1\": \"\",\n \"street2\": \"\",\n \"city\": \"\",\n \"state\": \"\",\n \"postalCode\": \"\",\n \"country\": \"\",\n \"domain\": \"http://{Your_Site}xample.org\",\n \"action\": \"addUser\",\n \"token\": \"\",\n \"transId\": \"1413290890.usr.209883490\",\n \"customerId\": \"145qjk345kl_908jkl.345\",\n \"src_name\": \"Your_Application\",\n \"channel\": \"webpage\",\n \"accountId\": \"0097892hjke6987hiuw.ACNT.hsapou8972rjk\",\n \"system\": \"Your_System\",\n \"originatingSystem_code\": \"Your_System_Id\",\n \"purchase_currency\": \"USD\",\n \"url\": \"https://another-link-to-my-example.org/add-user/new\",\n \"createFlag\": \"on\",\n \"web_version\": \"7\",\n
如果我可能错过了,请询问任何其他信息。
解决方案
使用[:alnum:]
字符类或类似的而不是\w
括号内的表达式。
我强调了以下更改:
s/(ftp|http[s]?):\/\/([\w\.-]+)/\1{Your_Site}/gI
^^
s/(ftp|http[s]?):\/\/([[:alnum:]\.-]+)/\1{Your_Site}/gI
^^^^^^^^^
请注意,此 RE 仍然过于宽松,并且会匹配无效名称,以防万一。
推荐阅读
- javascript - 来自更高层次的 jQuery 内容?
- leaflet - 使用自定义模块在 Drupal 上的组外标记上使用控制层时出现问题
- embedded - 无法在 yocto 中将文件复制到我的目标设备
- amazon-web-services - 什么是 setSdkClientExecutionTimeout 的 AWS SDKV2 等效项
- flutter - onChange TextField 如何将数据传递到按钮的 onPressed 属性中
- java - Spring Boot 中的配置文件特定日志记录
- python - 如何在 csv 文件中绘制多个跟踪对象的坐标(每个坐标都有单独的列)
- android - Android:以编程方式从本地存储安装 .apks/xapk(多个 .apk 文件)?
- php - ACF 帖子类别 – 首次发布的帖子未保存
- php - 上传图像时“字段列表”中的未知列“数组”