0

I have a problem replacing strings in python.

I have several files that contain certain string i.e. eam.

so if I have the files

scream.srt and scream eam.txt

when I run the code to replace or remove eam I end up with scrcream (Replace) or scr.srt/scr.srt

How do I target only the eam part not the "EAM" in Scream.

string_tofind
string_replacewith

  for files in glob.glob("*"):
        filename = os.path.splitext(files)
        ext = filename[-1]
        thefile = filename[0]
        print(filename[0])
        if string_tofind in thefile: 
            print("String Found")
            change = thefile.replace(string_tofind , string_replacewith) + ext
            print(change)
            os.rename(files, change)

        else:
            print("String not found")
petezurich
  • 9,280
  • 9
  • 43
  • 57
Tpd
  • 25
  • 5
  • `change = re.sub("^" + string_tofind, string_replacewith, thefile)`. And [this](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) is how it works. – Ignatius Reilly Jul 14 '22 at 05:34

2 Answers2

-1

Assuming your question can be simplified to: how to I replace every occurrence of "eam" with repl in a string that is not part of a bigger word. Well, here is a function that does that.

import re
pat = re.compile(r"\beam\b")

def replace_not_in_word(string, repl):
    return pat.sub(repl, string)

And, indeed, here is the result:

>>> replace_not_in_word("scream eam.txt", "X")
'scream X.txt'
>>> replace_not_in_word("scream.srt", "Y")
'scream.srt'
>>>

Note that we also precompile the regular expression, which leads to better performance.

jthulhu
  • 7,223
  • 2
  • 16
  • 33
-2

You can try to use regex to achieve it.

The re module provides regular expression matching operations.


/eam+?(?=\.)/

e matches the character e literally (case sensitive)

a matches the character a literally (case sensitive)

m matches the character m literally (case sensitive)

+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)

Positive Lookahead (?=\.)

Assert that the Regex below matches

\. matches the character . literally (case sensitive)


import re

str1 = "scream.srt";
str2 = "scream eam.txt";

def format(str):
    regex = r"eam+?(?=\.)"
    return re.sub(regex, "", str)
   
# scr.srt
print(format(str1))

# scream .txt
print(format(str2))
Ian
  • 1,198
  • 1
  • 5
  • 15
  • This is ***not*** what is asked for. `scream.srt` should stay like that... – Tomerikoo Jul 14 '22 at 06:46
  • @Tomerikoo Because the question does not ask to replace the empty string as well, only the special note `target only the eam part` – Ian Jul 14 '22 at 06:53
  • An empty string conforms to the filename rules. If there are no additional requirements, it is best not to do additional things. Regardless of the result, it is useless if it is different from the requirement. – Ian Jul 14 '22 at 06:56