Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: replace exact match using a variable

Tags:

python

regex

Program reads a text file and replaces a matched word based on a variable. The problem is that non-exact matches are being replaced. So if I'm replacing "the" with "da" the word "then" becomes "dan"

for fn in os.listdir('.'):
 if os.path.isfile(fn):
    if fn.endswith(".txt"):

        s = open(fn).read()

        for i in skills:
            link = skills[i]        

            s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1)

        print "%s updated" %fn

        f = open(fn, 'w')
        f.write(s)
        f.close()

The issue is at s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1) where the first argument in s.replace() is the variable containing the string I want to match for.

I've tried using word boundaries s = s.replace(r'\b'+i+r'\b', "<a href=\"%s\">%s</a>" %(link,i), 1)

and formatting the value at i into a new variable: regex = r'\b' + i + r'\b' and using regex as the first argument in s.replace but it doesn't work.

Note. skills is a dictionary containing word/link pairs

like image 335
dk1 Avatar asked Jan 29 '15 02:01

dk1


1 Answers

Since s is a string, it won't support regular expression operations.

To use regular expressions on your strings, you need to do that explicitly.

So put at the start of your code

import re

and where you now use

s = s.replace(i, "<a href=\"%s\">%s</a>" %(link,i), 1)

use, instead,

s = re.sub(r'\b'+i+r'\b', '<a href="%s">%s</a>'%(link,i), s, 1)
like image 182
Alex Martelli Avatar answered Sep 23 '22 14:09

Alex Martelli