mathsbeauty
mathsbeauty

Reputation: 366

Replacing every nth occurrence of a string by n + string in text file

A text file contains

This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo

The required output is

This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo 

So the problem is to replace nth occurrence of ABC XYZ by n ABC XYZ.

Upvotes: 0

Views: 281

Answers (3)

Olivier Melançon
Olivier Melançon

Reputation: 22294

The method re.sub can take a function as second argument. Use a stateful function with an itertools.count object as counter.

Code

import re, itertools

s = 'This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo'

def enumerator():
    counter = itertools.count(1)

    return lambda m: '{} {}'.format(next(counter), m.group())

out = re.sub(r'ABC XYZ', enumerator(), s)

print(out)

The function enumerator can be reused for any pattern.

Output

This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo

Upvotes: 2

mb0850
mb0850

Reputation: 593

Code:

import re

text = "This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo"
x = re.split("(ABC XYZ)",text)
c=0
for i,s in enumerate(x):
    if re.match('(ABC XYZ)',x[i]):
        c+=1
        x[i] = str(c)+' '+x[i]
x = ''.join(x)   # This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo

You can use more optimized ways of doing this, however this would help you understand it better.

Upvotes: 0

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

You could use a list comprehension

a="This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo"
''.join([e+str(c+1)+" ABC XYZ" for c,e in enumerate(a.split("ABC XYZ"))][0:-1])+a.split("ABC XYZ.")[-1]

Upvotes: 1

Related Questions