【问题标题】:how to do python word replacement?如何进行python单词替换?
【发布时间】:2017-10-06 04:48:33
【问题描述】:

我想从字符串中替换一些 word。例如。 'eq' 到 '=','gt' 到 '>'

举个例子:

s = "name eq 'alex' and age gt 36" to "name = 'alex' and age > 36"

我不能使用 string.replace() 因为它会替换字符。 我应该拆分句子然后re.sub(r'^eq$', '=', s)吗?是否有捷径可寻?

ps。我不想做s.replace(' eq ', ' = '),因为字符串的开头或结尾可能有单词。例如,将 sender eq me 中的 me 替换为 'alex' 应该会导致 sender = 'alex'

【问题讨论】:

  • "不能使用 string.replace(),因为它会替换字符"
  • 我不太确定您不想使用 string.replace() 的原因。这似乎是您想要实现的目标的完美解决方案
  • 他可能的意思是他不希望像equity 这样的东西变成=uity
  • 如果eqgtlt分词表示,使用string.replace是没有问题的。
  • @bulbus OP 说“我不想做 s.replace(' eq ', ' = ') 因为字符串的开头或结尾可能有单词”我认为这意味着它并不总是以空格分隔。那么预期的输出是a eq beqe = b'a'= b(a)= b

标签: python regex string replace


【解决方案1】:

使用正则表达式替换。它将避免误导性影响。

import re

s = ("name eq 'alex' and age > 36 equity eq\n"
    "{ dictionary: \"\" } eq { dictionary: \"\" }\n"
    "\"string\" eq \"string\"\n"
    "var eq var\n"
    "[\"list\"] eq [\"list\"]\n"
    "(\"tuple\") eq (\"tuple\")")

regex_template_start = r"(?<=[' \"\w\[\]\(\)\{\}])\b"
regex_template_end   = r"\b(?=[' \"\w\[\]\(\)\{\}])"

s = re.sub(r"{0}{1}{2}".format(regex_template_start, "eq", regex_template_end), '=', s)
s = re.sub(r"{0}{1}{2}".format(regex_template_start, "gt", regex_template_end), '>', s)

print(s)

查看结果:https://repl.it/MLpM/5


说明:

Python 变量规则 变量名必须以字母或 下划线,如:

  • _下划线
  • 下划线_

变量名的其余部分可能由字母、数字组成 和下划线。

  • 密码1
  • n00b

  • un_der_scores

所以正则表达式必须涵盖变量名称的情况。 此外,它可以是元组、字典或列表,因此正则表达式也包括[]{}()

【讨论】:

  • 你能解释一下吗?
  • @Mengo 查找“环视”! regular-expressions.info/lookaround.html
  • @bulbus 我可能会选择(?&lt;=[^0-9a-zA-Z_])eq(?=[^0-9a-zA-Z_])
  • 我觉得你可以在聊天区讨论这个问题,cmets 不是个好办法。
【解决方案2】:

看来replace 是完美的解决方案。

s = "name eq 'alex' and age > 36" 
goal = "name = 'alex' and age > 36"
s = s.replace(" eq ", " = ")

s == goal

【讨论】:

  • 如果运算符也可以位于字符串的边缘,则可以显式检查开始和结束,而不是对整个字符串进行操作。
【解决方案3】:

您可以使用简单的 for 循环来避免更改 'eq' 和 'gt' 的其他实例

# split on space character
words = s.split(' ')    

# loop and check every word
for i, w in enumerate(words):    

    # replace occurrence of r'^eq$'
    if w == 'eq':    
        words[i] = '='

    # replace occurrence of r'^gt$'
    elif w == 'gt':     
        words[i] = '>'

s = ' '.join(words)

【讨论】:

  • 去掉行间不必要的空格。
【解决方案4】:

只需使用 re.sub 函数

import re
s = "name eq 'alex' with equity gt 36"
s = re.sub(r'\s{1}eq\s{1}', ' = ', s)
s = re.sub(r'\s{1}gt\s{1}', ' > ', s)
print(s)

输出是 name = 'alex' with Equity > 36 正是你想要的

【讨论】:

    【解决方案5】:

    一种解决方案是创建一个包含所有需要替换的值的key:value 的字典。 将字符串拆分为列表可以解决诸如

    之类的单词的问题
    equity
    其中还包含单词eq
    dicta = {
        "eq" : "=",
        "gt" : ">",
        "lt" : "<"
    }
    

    我尝试替换字典中列表中的单词。它并不完美,而是完成此任务的另一种方式。我正在尝试更新它。

    s = "name eq 'alex' and age gt 36"
    [s.replace(char, dicta.get(char)) for char in s.split(" ") if char in dicta ]
    

    【讨论】:

      【解决方案6】:

      这里我们使用REGEX 来实现所需的输出。

      正则表达式: (?:(?&lt;=^)|(?&lt;=\s))eq(?=\s|$)

      1. 这将匹配eq 和正向向后看start of string ^space \s 和正向向前看end of string $space \s

      解决方案 1:

      Regex demo

      import re
      s = "name eq 'alex' and age > 36"
      print re.sub('(?:(?<=^)|(?<=\s))eq(?=\s|$)',"=",s)
      print re.sub('(?:(?<=^)|(?<=\s))gt(?=\s|$)',">",s)
      

      Try this code snippet here

      解决方案 2:

      您可以这样做,将space-eq-space 替换为space-=-space

      根据要求,您有问题eqeq 之后和之前都有space

      Try this code snippet here

      s = "name eq 'alex' and age > 36"
      print s.replace(" eq "," = ").replace(" gt ", " > ") 
      

      【讨论】:

      • 在评论中,OP 说明了为什么该方法不是我们想要的,因为它可以修改“eg”或“gt”的其他实例,例如在“equity”中,如@flakes
      • 我也不知道对这个优雅的解决方案投反对票的原因是什么!它似乎比其他解决方案更好。不过可以在链的末尾添加.replace(" lt ", " &lt; ")
      • @SanyamMehra 在这样的情况下,我们应该添加对遇到此类模式问题的每个人都有帮助的答案,而不是仅仅查看特定案例。
      • @SanyamMehra 当一件事可以通过 inbuit 函数以优雅的方式完成时,我们为什么要使用 REGEX 以复杂的方式移动,这是我的意见,否则由你决定.. :)跨度>
      • 请参考问题:“ps。我不想做 s.replace('eq', '=') 因为字符串的开头或结尾可能有单词。”您的建议不能满足 OP 的要求。
      猜你喜欢
      • 2020-03-27
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2018-10-30
      • 2015-09-11
      • 2012-05-06
      相关资源
      最近更新 更多