簡體   English   中英

查找模式的所有匹配項並將其替換為文本

[英]Find all matches of a pattern and replace them in a text

我有一個如下的模式:

measurement = re.compile("(\d+(?:\.\d*)?)\s*x\s*(\d+(?:\.\d*)?)\s*(cm|mm|millimeter|centimeter|millimeters|centimeters)")

可以在句子和文檔中多次看到它。 我想找到所有匹配項,並將其替換為“ MEASUREMENT”,也想在列表中添加其值。

**Input_Text**: measuring 9 x 5 mm and previously measuring 8 x 6 mm

**Output**: measuring MEASUREMENT and previously measuring MEASUREMENT

**List**: 9 x 5 mm, 8 x 6 mm

到目前為止,我的代碼在下面,但僅帶來第一個匹配項:

result = re.search(measurement, Input_Text)
                    if result:
                        Input_Text = Input_Text.replace(result, "MEASUREMENT") 

您可以使用re.sub()進行替換,並使用re.findall()獲得所有匹配的字符串。

measurement = re.compile("(\d+(?:\.\d*)?)\s*x\s*(\d+(?:\.\d*)?)\s*(cm|mm|millimeter|centimeter|millimeters|centimeters)")

text = "measuring 9 x 5 mm and previously measuring 8 x 6 mm"

values = re.findall(pattern=measurement, string=text)

sub_text = re.sub(pattern=measurement, string=text, repl='MEASUREMENT')

>>> sub_text
'measuring MEASUREMENT and previously measuring MEASUREMENT'

>>> values
[('9', '5', 'mm'), ('8', '6', 'mm')]

如果您不想兩次解析字符串,則可以將re.sub與函數作為替換參數一起使用。 使用此功能,您可以輕松地填充匹配字符串的列表。

pat = re.compile(r'\d+(?:\.\d*)?\s*x\s*\d+(?:\.\d*)?\s*(?:cm|mm|millimeters?|centimeters?)')

s = r'measuring 9 x 5 mm and previously measuring 8 x 6 mm'

l = []

def repl(m):
    l.append(m.group(0))
    return 'MEASUREMENT'

s = pat.sub(repl, s)

暫無
暫無

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

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