2016-01-13 51 views
2

我有一定的串fruits这样:附加到行

Apples 
Bananas 
Pineapples 

有一个在每一行的末尾的回车\r,以及在开始和结束字符串。使用正则表达式,我将如何去追加: 1到第一行Apples的结尾?我曾尝试以下无济于事:

re.sub("Apples(\r)", "\1:1", fruits) 

我的想法是,\1应该更换什么是在括号(\r),但一切都在模式所取代。

回答

2

您正在做的是匹配"Apples\r",在此过程中捕获"\r",然后用"\r:1"替换整个匹配。

对于这个简单的例子,不需要捕获匹配到\r,无论如何,因为唯一能匹配它的是\r。您可以将其硬编码到替换字符串中。

我假设你想得到的字符串是"\rApples: 1\rBananas\rPineapples\r

您可以使用一个lookbehind使Apples没有被消耗(虽然我听说,每天食用一个远离医生):

re.sub("(?<=Apples)\r", ": 1\r", fruits) 

但你也可能只是这样做:

re.sub("Apples\r", "Apples: 1\r", fruits) 

如果您想在每个水果后添加: 1,那么后向将更加有用:

re.sub("(?<=[^\r])\r", ": 1\r", fruits) 

上面说的是找到每个\r,它跟随的字符不是\r,而是用: 1\r替换它们。那么结果将是:

# \rApples: 1\rBananas: 1\rPineapples: 1\r\r 
1

如果你

re.sub("A(B)", "\1C", "AB") 

你会得到BC,因为\1是由什么是在支架更换

要获得AC,你应该做的:

re.sub("(A)B", "\1C", "AB") 
0

有\ R作为你的水果分离使得很难打印出来的东西;所以为了这个答案的目的,我将在它的位置使用@字符。如果您将\ r分配给我的分隔符变量并使用实际的\ r分隔字符串作为fruit_str,则后面的代码工作原理相同。

一些解释遵循代码。

import re 

def updateFruitQuantity(the_fruit, the_quantity, fruit_str, separator): 
    re_1 = r"(" + the_fruit + r")(:.*?|)" + separator 
    re_2 = r'\1:' + str(the_quantity) + separator 
    fruit_str = re.sub(re_1, re_2, fruit_str) 
    return(fruit_str) 

separator = "@" 
fruit_str = "@[email protected]@[email protected]" 
print(fruit_str) 

fruit_str = updateFruitQuantity("Pineapples", 25, fruit_str, separator) 
print(fruit_str) 
fruit_str = updateFruitQuantity("Bananas", 17, fruit_str, separator) 
print(fruit_str) 
fruit_str = updateFruitQuantity("Pineapples", 3, fruit_str, separator) 
print(fruit_str) 
fruit_str = updateFruitQuantity("Apples", 94, fruit_str, separator) 
print(fruit_str) 
fruit_str = updateFruitQuantity("Apples", 102, fruit_str, separator) 
print(fruit_str) 

这里是代码的输出:

@[email protected]@[email protected] 

@[email protected]@Pineapples:[email protected] 
@[email protected]:[email protected]:[email protected] 
@[email protected]:[email protected]:[email protected] 
@Apples:[email protected]:[email protected]:[email protected] 
@Apples:[email protected]:[email protected]:[email protected] 

我要建立单独的正则表达式的目标文本和替换文本。

这些目标表达式假定每个fruit:quantity后面跟着分隔符。目标表达式中有两个捕获组 - 每个都被括号包围。第二个分组在目标表达式中非常重要,因为它可以清除任何可能存在的数量元素。

替换表达式以\ 1开头,代表与目标表达式中第一个分组相匹配的文本(例如“Apples”)。接下来是冒号,然后是要使用的数量字符串。这样做可以确保任何现有的:数量可以用新数量正确替换,并且在没有现有数量的情况下也可以使用。因此,例如,在我们的第三次更改中,您看到菠萝的数量从25回到3.

随着时间的推移,您需要另一种机制为fruit_str添加新的水果类型。