Python:在同一过程中计算并替换正则表达式?

Python:在同一过程中计算并替换正则表达式?,python,regex,Python,Regex,我可以用re.sub()全局替换正则表达式,并且可以用 for match in re.finditer(): count++ 有没有一种方法可以将这两者结合起来,这样我就可以在不通过源字符串两次的情况下计算替换数 注意:我对替换是否匹配不感兴趣,我感兴趣的是同一调用中匹配的精确计数,避免一次调用计数和一次调用替换。在调用re.sub函数时,可以传递repl函数。 函数接受单个匹配对象参数,并返回替换字符串。repl函数用于模式的每个非重叠出现 试试这个: count = 0 def coun

我可以用
re.sub()
全局替换正则表达式,并且可以用

for match in re.finditer(): count++
有没有一种方法可以将这两者结合起来,这样我就可以在不通过源字符串两次的情况下计算替换数


注意:我对替换是否匹配不感兴趣,我感兴趣的是同一调用中匹配的精确计数,避免一次调用计数和一次调用替换。

在调用
re.sub
函数时,可以传递
repl
函数。 函数接受单个匹配对象参数,并返回替换字符串。
repl
函数用于模式的每个非重叠出现

试试这个:

count = 0
def count_repl(mobj): # --> mobj is of type re.Match
    global count
    count += 1 # --> count the substitutions
    return "your_replacement_string" # --> return the replacement string

text = "The original text" # --> source string
new_text = re.sub(r"pattern", repl=count_repl, string=text) # count and replace the matching occurrences in one pass.
count = 0
def count_repl(mobj):
    global count
    count += 1
    return f"ID: {mobj.group(1)}"

text = "Jack 10, Lana 11, Tom 12, Arthur, Mark"
new_text = re.sub(r"(\d+)", repl=count_repl, string=text)

print(new_text)
print("Number of substitutions:", count)
Jack ID: 10, Lana ID: 11, Tom ID: 12
Number of substitutions: 3
或,

您可以使用它执行与相同的操作,但返回元组(新的\u字符串、生成的\u子的数量)


示例:

count = 0
def count_repl(mobj): # --> mobj is of type re.Match
    global count
    count += 1 # --> count the substitutions
    return "your_replacement_string" # --> return the replacement string

text = "The original text" # --> source string
new_text = re.sub(r"pattern", repl=count_repl, string=text) # count and replace the matching occurrences in one pass.
count = 0
def count_repl(mobj):
    global count
    count += 1
    return f"ID: {mobj.group(1)}"

text = "Jack 10, Lana 11, Tom 12, Arthur, Mark"
new_text = re.sub(r"(\d+)", repl=count_repl, string=text)

print(new_text)
print("Number of substitutions:", count)
Jack ID: 10, Lana ID: 11, Tom ID: 12
Number of substitutions: 3
输出:

count = 0
def count_repl(mobj): # --> mobj is of type re.Match
    global count
    count += 1 # --> count the substitutions
    return "your_replacement_string" # --> return the replacement string

text = "The original text" # --> source string
new_text = re.sub(r"pattern", repl=count_repl, string=text) # count and replace the matching occurrences in one pass.
count = 0
def count_repl(mobj):
    global count
    count += 1
    return f"ID: {mobj.group(1)}"

text = "Jack 10, Lana 11, Tom 12, Arthur, Mark"
new_text = re.sub(r"(\d+)", repl=count_repl, string=text)

print(new_text)
print("Number of substitutions:", count)
Jack ID: 10, Lana ID: 11, Tom ID: 12
Number of substitutions: 3
你可以用

它返回
(新字符串、所做接头的编号)

例如,我使用的示例与@Shubham Sharma使用的示例相同

text = "Jack 10, Lana 11, Tom 12, Arthur, Mark"
out_str,count=re.subn(r"(\d+)", repl='replacement', string=text)

#out_str-->'Jack replacement, Lana replacement, Tom replacement, Arthur, Mark'
#count---> 3
好的,有更好的办法
如问题中所述,我对是否进行了替换不感兴趣,我感兴趣的是(A)知道进行了多少次替换,以及(b)通过进行替换的同一电话进行替换。