【问题标题】:Replacing a string with another using regex in Python在 Python 中使用正则表达式将字符串替换为另一个字符串
【发布时间】:2018-03-20 23:20:40
【问题描述】:

我正在尝试使用正则表达式将所选文本替换为所选文本中的一个单词。我尝试了 re.sub() 但它似乎将第二个参数“我想用文本替换它的单词”作为字符串,而不是正则表达式。

这是我的字符串:

I go to Bridgebrook i go out <ERR targ=sometimes> some times </ERR> on Tuesday night i go to Youth <ERR targ=club> clob </ERR> .

这是我的代码:

# The regex of the form <ERR targ=...> .. </ERR>
select_text_regex = r"<ERR[^<]+<\/ERR>"

# The regex of the correct word that will replace the selected text of teh form <ERR targ=...> .. </ERR>
correct_word_regex = r"targ=([^>]+)>"
line = re.sub(select_text_regex, correct_word_regex, line.rstrip())

我明白了:

I go to Bridgebrook i go out targ=([^>]+)> on Tuesday night i go to
Youth targ=([^>]+)> .

我的目标是:

I go to Bridgebrook i go out sometimes on Tuesday night i go to
Youth club .

Python 是否支持使用 Regex 替换两个字符串?

【问题讨论】:

    标签: python regex python-3.x


    【解决方案1】:

    这是另一个解决方案(我还使用“非贪婪”修饰符重写了正则表达式,将? 放在* 之后,因为我发现它更具可读性)。

    r"\1" 引用的组以括号作为未命名组。还使用 re.compile 作为样式偏好以减少 args 的数量:

    line = "I go to Bridgebrook i go out <ERR targ=sometimes> some times </ERR> on Tuesday night i go to Youth <ERR targ=club> clob </ERR> ."
    select_text_regex = re.compile(r"<ERR targ=(.*?)>.*?<\/ERR>")
    select_text_regex.sub(r"\1", line)
    

    命名组替代:

    line = "I go to Bridgebrook i go out <ERR targ=sometimes> some times </ERR> on Tuesday night i go to Youth <ERR targ=club> clob </ERR> ."
    select_text_regex = re.compile(r"<ERR targ=(?P<to_replace>.*?)>.*?<\/ERR>")
    select_text_regex.sub(r"\g<to_replace>", line)
    

    您可以在此处找到有关组引用的一些文档:

    https://docs.python.org/3/library/re.html#regular-expression-syntax

    【讨论】:

    • 请记住,这只是一个正则表达式解决方案!如果您的用例可能更复杂,很多人会建议您使用适当的解析器和像 beautifulsoup 这样的库:crummy.com/software/BeautifulSoup/bs4/doc
    • 是否可以用re.sub()将替换后的文字转成大写字母?
    • 此解决方案有效 - 将 "r\1" 替换为函数:stackoverflow.com/questions/8934477/…
    • 我使用了你的第二个实现:line = select_text_regex.sub(r"\g&lt;to_replace&gt;\1",lambda m: m.group('first').upper(), line) 它说:TypeError: 'str' object cannot be interpreted as an integer
    【解决方案2】:

    您需要匹配模式中的目标词,作为捕获组 - 您不能在替换字符串中开始全新的搜索!

    未经测试,但这应该可以完成工作:

    替换r"&lt;ERR targ=(.*?)&gt;.*?&lt;/ERR&gt;"

    与r"\1"

    【讨论】:

      【解决方案3】:

      您正在寻找的是正则表达式捕获组。不要选择正则表达式,然后尝试用另一个正则表达式替换它,而是将要匹配的正则表达式部分放在 select 语句中的括号内,然后用 \1 将其取回替换。 (数字是您包含的组)

      line = "I go to Bridgebrook i go out <ERR targ=sometimes> some times </ERR> on Tuesday night i go to Youth <ERR targ=club> clob </ERR> ."
      
      select_text_regex = r"<ERR targ=([^<]+)>[^<]+<\/ERR>" #Correct Here.
      correct_word_regex = r"\1" #And here.
      
      line = re.sub(select_text_regex, correct_word_regex, line.rstrip())
      
      print(line)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-20
        • 2022-11-07
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 2013-06-13
        相关资源
        最近更新 更多