regex 为什么这个正则表达式不适用于这些捕获组?[已关闭]

pn9klfpd  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(107)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
我使用了一个组模式来识别文本中的两种情况,例如“numberletter”和“lowercase letter-capital letter”。第二种模式在这里没有出现,但我想把它保存下来以备将来使用。所以我想创建一个名为“pattern 1”的变量,它将我需要的所有模式进行分组,并且可以在其他时候使用组索引\1,\2,\3进行调用。

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.'

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

qv7cva1a

qv7cva1a1#

import re

text = "There are 2examples like this 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)

相关问题