簡體   English   中英

如何在Python中使用正則表達式在字符串中進行以下替換?

[英]How to make the following substitution in string using regex in Python?

我正在嘗試在以下字符串中進行替換:

poem='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
'''

要求如下所示:

  1. 如果模式包含字符'ai'或'hi',則將后三個字符替換為*\\*
  2. 如果單詞有“ ch”或“ co”,請用“ Ch”或“ Co”代替。

我嘗試了以下方法:

print(re.sub(r"ai\w{3}|hi\w{3}",r"(ai|hi)*\*",poem))    

輸出:

If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one f(ai|hi)*\*ng robin
Unto his nest again,
I shall not live in vain.

print(re.sub(r"ch|co",r"Ch|Co",poem))

輸出:

If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aCh|Coing,
Or Ch|Cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.

您可以看到輸出不符合要求。 請幫助我找到正確的正則表達式。

首先,您可以通過從替換中的模式引用捕獲的組來實現:

poem = re.sub(r"(ai|hi)\w{3}", "\g<1>*\*", poem)

對於第二個,您可以傳遞一個函數作為替換(請參閱re.sub docs ):

def title(match):
    return match.group(0).title()  # or .capitalize()

poem = re.sub(r"ch|co", title, poem)

您可以逐步替換這些:

poem='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
'''

import re

p2 = re.sub("(?:ai|hi)...","*/*",poem)
p3 = re.sub("ch","Ch",p2)
p4 = re.sub("co","Co",p3)

print(p4)

輸出:

If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the ac*/*
Or Cool one pain,
Or help one f*/*ng robin
Unto */*est again,
I shall not live in vain.

唯一有趣的是ai | hi周圍的非捕獲組無法按我預期的那樣工作-ai和hi仍被替換。 您可能需要將它們更改為:

p = re.sub("ai...","*/*",poem, flags = re.DOTALL)
p2 = re.sub("hi...","*/*",p, flags= re.DOTALL)
p3 = re.sub("ch","Ch",p2)
p4 = re.sub("co","Co",p3)

print(p4)

輸出:

If I can stop one heart from breaking,
I shall not live in v*/*If I can ease one life the ac*/*
Or Cool one p*/*Or help one f*/*ng robin
Unto */*est ag*/*I shall not live in v*/*

標志re.DOTALL使. 還匹配換行符。 沒有它, vain; 將不匹配。

import re
poem = re.sub(r'(ai|hi)(...)', r'\1*\*', poem)
poem = re.sub('ch', 'Ch', poem)
poem = re.sub('co', 'Co', poem)
print(poem)

輸出:

If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aChi*\*
Or Cool one pain,
Or help one fai*\*ng robin
Unto hi*\*est again,
I shall not live in vain.

print(re.sub(r"co",r"Co",re.sub(r"ch",r"Ch",s)))

這有效:

Input:

    s='''It takes strength for being certain,
    It takes courage to have doubt.
    It takes strength for challenging alone,
    It takes courage to lean on another.
    It takes strength for loving other souls,
    It takes courage to be loved.
    It takes strength for hiding our own pain,
    It takes courage to help if it is paining for someone.'''

    Output:
    It takes strength for being certain,
    It takes Courage to have doubt.
    It takes strength for Challenging alone,
    It takes Courage to lean on another.
    It takes strength for loving other souls,
    It takes Courage to be loved.
    It takes strength for hiding our own pain,
    It takes Courage to help if it is paining for someone.

這是您問題的答案:
import re

poem='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
'''`

p1=poem
print(re.sub(r"\n","",poem))
poem=re.sub(r"co","Co",poem)
poem=re.sub(r"ch","Ch",poem)
print(poem)
print(re.sub(r"ai|hi{3}","*/*",p1))`

您可以使用| 用作or提及選項並使用()創建組以通過在替換字符串中使用\\<group number> (1索引)匹配並保留某些組

對於第一個,您可以使2個組匹配(hi|ai)然后匹配后3個字符,例如(...) ,然后僅替換第二組並使用\\1保留第一個組

print(re.sub(r'(hi|ai)(...)', r'\1*\*', poem))

對於第二個,您可以使2個組匹配(c)(h|o)並使用\\2保留第二個組

print(re.sub(r'(c)(h|o)', r'C\2', poem))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM