为什么这个正则表达式不适用于这些捕获组?

问题描述 投票:0回答:1

我使用组模式来识别文本中的两种情况,例如“numberletter”和“lowercase letter-capital letter”。第二种模式在这里没有出现,但我想把它买下来以备将来使用。 所以我想创建一个名为“pattern1”的变量,它将我需要的所有模式分组,并且可以在其他时间使用组索引调用。

txt = '10.3Requirement - Operation of rainwater installations. 10.4Collect and conduct rainwater. 10.5Criterion - Sizing.'

pattern1 = re.compile(r"((\d)([A-Z]))(([a-z]) ([A-Z]))")
pattern1.sub(r'\2 \3', txt)

结果:

'10.3Requirement - Operation of rainwater installations. 10.4Collect and conduct rainwater. 10.5Criterion - Sizing.'

数字和大写字母之间有空格的预期文本:

'10.3 Requirement - Operation of rainwater installations. 10.4 Collect and conduct rainwater. 10.5 Criterion - Sizing.'

为什么没有替换第一个数字和大写字母的模式?

python regex regex-group
1个回答
0
投票
import re

text = "There are 2examples like 1this and Here in the text."

pattern = re.compile(r"(\d)([a-zA-Z])|([a-z]) ([A-Z])") # You need to use | which is the or operator.
replacement = (
    lambda m: m.group(1) + " " + m.group(2)
    if m.group(1)
    else m.group(3) + ". " + m.group(4)
) # And this to give different results based on which group was found, I made it to add a period for the second case.

modified_text = pattern.sub(replacement, text)
print(modified_text)
© www.soinside.com 2019 - 2024. All rights reserved.