pb100
pb100

Reputation: 786

Replace part of string using python regular expression

I have the following lines (many, many):
...
gfnfgnfgnf: 5656756734
arvervfdsa: 1343453563
particular: 4685685685
erveveersd: 3453454545
verveversf: 7896789567
..

What I'd like to do is to find line 'particular' (whatever number is after ':') and replace this number with '111222333'. How can I do that using python regular expressions ?

Upvotes: 0

Views: 3786

Answers (6)

freegnu
freegnu

Reputation: 813

Your sed example forces me to say neat!

python -c "import re, sys; print ''.join(re.sub(r'^(particular:) \d+', r'\1 111222333', l) for l in open(sys.argv[1]))" file

Upvotes: 0

Katriel
Katriel

Reputation: 123622

An important point here is that if you have a lot of lines, you want to process them one by one. That is, instead of reading all the lines in replacing them, and writing them out again, you should read in a line at a time and write out a line at a time. (This would be inefficient if you were actually reading a line at a time from the disk; however, Python's IO is competent and will buffer the file for you.)

with open(...) as infile, open(...) as outfile:
    for line in infile:
        if line.startswith("particular"):
            outfile.write("particular: 111222333")
        else:
            outfile.write(line)

This will be speed- and memory-efficient.

Upvotes: 0

Andrew Sledge
Andrew Sledge

Reputation: 10351

input = """gfnfgnfgnf: 5656756734
arvervfdsa: 1343453563
particular: 4685685685
erveveersd: 3453454545
verveversf: 7896789567"""

entries = re.split("\n+", input)

for entry in entries:
  if entry.startswith("particular"):
    entry = re.sub(r'[0-9]+', r'111222333', entry)

or with sed:

sed -e 's/^particular: [0-9].*$/particular: 111222333/g' file

Upvotes: 0

Paweł Nadolski
Paweł Nadolski

Reputation: 8474

#!/usr/bin/env python
import re

text = '''gfnfgnfgnf: 5656756734
arvervfdsa: 1343453563
particular: 4685685685
erveveersd: 3453454545
verveversf: 7896789567'''

print(re.sub('[0-9]+', '111222333', text))

Upvotes: 2

Paulo Scardine
Paulo Scardine

Reputation: 77251

Sure you need a regular expression?

other_number = '111222333'
some_text, some_number = line.split(': ')
new_line = ': '.join(some_text, other_number)

Upvotes: 3

jonesy
jonesy

Reputation: 3542

for line in input:
    key, val = line.split(':')
    if key == 'particular':
       val = '111222333'

I'm not sure regex would be of any value in this specific case. My guess is they'd be slower. That said, it can be done. Here's one way:

for line in input:
    re.sub('^particular : .*', 'particular : 111222333')

There are subtleties involved in this, and this is almost certainly not what you'd want in production code. You need to check all of the re module constants to make sure the regex is acting the way you expect, etc. You might be surprised at the flexibility you find in dealing with problems like this in Python if you try not to use re (of course, this isn't to say re isn't useful) ;-)

Upvotes: 3

Related Questions