0

I need to write a regex, that will traverse through my file and replace each :

href='#!/api/API.

with

href='http://www.domain.com/API. . I currently have this kind of code, that will be run from console :

def replace(file, pattern, substring):
    my_file = open(file)
    for line in my_file:
        my_file.write(line.replace(pattern, substring))

Will sending the given strings work in this case ?

2
  • 2
    Can't you just use the standard search/replace functionality in your editor?
    – Bojangles
    Mar 22, 2012 at 1:54
  • Yes/no questions are no good fit for this site, because all the possible answers don't fulfill the minimum length requirements.
    – Niklas B.
    Mar 22, 2012 at 2:11

1 Answer 1

1

So you want to replace #!/api with http://www.domain.com?

You can just do:

line.replace('#!/api','http://www.domain.com')

No need for regex.

2
  • So that's a "yes", I guess :)
    – Niklas B.
    Mar 22, 2012 at 2:12
  • first I thought that the replace string will be more complicated, but it turns out it's not. thanks anyway Mar 22, 2012 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.