Skip to content Skip to sidebar Skip to footer

Python: Count And Replace Regular Expression In Same Pass?

I can globally replace a regular expression with re.sub(), and I can count matches with for match in re.finditer(): count++ Is there a way to combine these two, so that I can coun

Solution 1:

You can use re.subn.

re.subn(pattern, repl, string, count=0, flags=0)

it returns (new_string, number_of_subs_made)

For example purposes, I'm using the same example as @Shubham Sharma used.

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

Solution 2:

You can pass a repl function while calling the re.sub function. The function takes a single match object argument, and returns the replacement string. The repl function is called for every non-overlapping occurrence of pattern.

Try this:

count = 0defcount_repl(mobj): # --> mobj is of type re.Matchglobal count
    count += 1# --> count the substitutionsreturn"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.

OR,

You can use re.subn which performs the same operation as re.sub, but return a tuple (new_string, number_of_subs_made).

new_text, count = re.sub(r"pattern", repl="replacement", string=text)

Example:

count = 0defcount_repl(mobj):
    global count
    count += 1returnf"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)

Output:

Jack ID:10,Lana ID:11,Tom ID:12Number of substitutions:3

Solution 3:

import re


text = "Jack 10, Lana 11, Tom 12"
count = len([x for x in re.finditer(r"(\d+)", text)])
print(count)

# Output: 3

Ok, there's a better way

import re


text = "Jack 10, Lana 11, Tom 12"
count = re.subn(r"(\d+)", repl="replacement", string=text)[1]
print(count)

# Output: 3

Post a Comment for "Python: Count And Replace Regular Expression In Same Pass?"