0

I have a long .txt file containing list of parameters in some given format. Let's say it look like this:

oneParameter = "definedStartSequenceOfTheParameter" + str(actualValueOfTheParameter) + "definedEndSequenceOfTheParameter"

My problem is that I would like to replace the value actualValueOfTheParameter. But I cannot use .replace method since I never actually know the value of actualValueOfTheParameter - it can be string, 10 digit float or 2 digit int, any value you can think of.

This brought me to a question whether there is perhaps some way to tell Python something like this:

find string which starts with "definedStartSequenceOfTheParameter" and ends with "definedEndSequenceOfTheParameter" while it does not matter how many or which characters are in between starting and ending string.

I know I could define some function containing for cycle but first I would like to have your opinion. So I don't shoot myself in the leg in case there exist some simple method or escape sequence that I do not know about.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Do you want the text in between if so let me know I believe I can help you – JACKDANIELS777 Mar 07 '21 at 10:39
  • The really interesting part, which none of the existing answers mention: may the given end sequence also be part of the actual value? – guidot Mar 07 '21 at 10:45
  • Does this answer your question? [Replace all text between 2 strings python](https://stackoverflow.com/questions/16159969/replace-all-text-between-2-strings-python) – Tomerikoo Mar 07 '21 at 11:18

3 Answers3

1

Use Regex sub(): https://docs.python.org/3/library/re.html#re.sub

s = "knownbeginKJLKYLHLKHDLLKDLBKBDKBknownend"
re.sub(r"(knownbegin).+(knownend)", r"\1" + replacenment + r"\2", s)
C14L
  • 12,153
  • 4
  • 39
  • 52
0

To build on C14L answer, use regex. Simple tool is to build a good regex is regex101

Here's a simple solution, I think you can figure out how to read text from a file on your own.

import re

testString = "definedStartSequenceOfTheParameterTheSpecificParameterdefinedEndSequenceOfTheParameter"

m = re.search('(?<=definedStartSequenceOfTheParameter)(.*)(?=definedEndSequenceOfTheParameter)',testString)
print(m.group(0))

Output:

TheSpecificParameter
Adilius
  • 308
  • 2
  • 10
-1

You could try the str.startswith() and str.endswith() methods.

Example:

string = "Hello"
if string.startswith("H") and string.endswith("o"):
    print("Yay!")
else:
    print("Nay!")
Ismail Hafeez
  • 730
  • 3
  • 10
  • Yeah, but in the question he asks: – Ismail Hafeez Mar 07 '21 at 10:44
  • find string which starts with "definedStartSequenceOfTheParameter" and ends with "definedEndSequenceOfTheParameter" while it does not matter how many or which characters are in between starting and ending string. – Ismail Hafeez Mar 07 '21 at 10:44
  • He never asked for the text in between. – Ismail Hafeez Mar 07 '21 at 10:45
  • You simplify the problem to a degree, where the similarity to the question is lost. Your code shows no simple enhancement path towards an extraction. – guidot Mar 07 '21 at 10:50
  • Hi! Thanks for your quick response. This would for sure work given the poor explanation I provided. However in the actual application I could not use this - but you had no way knowing and this really is the most simple way (after all such a rookie as me is also came up with this solution but was left stone faced when it did not work). Thank you though! – Daniel Řehák Mar 07 '21 at 10:52
  • No problem! Good luck with your project! – Ismail Hafeez Mar 07 '21 at 10:54