【问题标题】:script to replace text where replacing sting follow caption pattern of old替换文本的脚本,其中替换 sting 遵循旧的标题模式
【发布时间】:2016-05-28 23:44:59
【问题描述】:

我需要帮助来编写一个脚本,该脚本将使用目录结构中所有文件中的新文本字符串替换给定的文本字符串。 条件是新字符串也必须遵循旧字符串的标题模式。

例如 如果我想在项目中用“xyz”替换“abc”。它还必须搜索和替换以下模式 “XYZ”的“ABC” “Xyz”的“Abc” “XyZ”的“AbC” ..等等。

有人可以为此建议优化逻辑吗?

【问题讨论】:

  • 只有 A or a, B or b, C or c 吗?
  • ​​​​​​​​​​​​​​​​​​​Python正则表达式还是Bash?
  • 告诉我们你到目前为止尝试了什么,对于逻辑你可以使用正则表达式

标签: python regex bash shell


【解决方案1】:

重新导入

字符串 = "xYZ"

d = { 'X':'A' , 'Y':'B', 'Z':'C'}

对于字符串中的 c:

if c.isupper():

    string = re.sub(c,d[c],string)

else:

    string = re.sub(c,d[c.upper()].lower(),string)

打印字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多