0

I am trying to pull a substring out of a function result, but I'm having trouble figuring out the best way to strip the necessary string out using Python.

Output Example:

[<THIS STRING-STRING-STRING THAT THESE THOSE>]

In this example, I would like to grab "STRING-STRING-STRING" and throw away all the rest of the output. In this example, "[<THIS " &" THAT THESE THOSE>]" are static.

3
  • 1
    Are you asking how to split a string by white space and take the second entry? what have you tried? which part are you struggling with? Commented Jan 27, 2021 at 19:15
  • It really depends on the details of the string. If you know the length in characters before and after the part you want, that's easiest. In some cases you might be able to do clever stuff with .split(). More generally, you might want regular expressions. en.wikipedia.org/wiki/Regular_expression Commented Jan 27, 2021 at 19:17
  • maybe try regex: regexr.com/5l65c Commented Jan 27, 2021 at 19:22

4 Answers 4

1

Many many ways to solve this. Here are two examples:

First one is a simple replacement of your unwanted characters.

targetstring = '[<THIS STRING-STRING-STRING THAT THESE THOSE>]'

#ALTERNATIVE 1
newstring = targetstring.replace(r" THAT THESE THOSE>]", '').replace(r"[<THIS ", '')
print(newstring)

and this drops everything except your target pattern:

#ALTERNATIVE 2
match = "STRING-STRING-STRING"
start = targetstring.find(match)
stop = len(match)
targetstring[start:start+stop]

These can be shortened but thought it might be useful for OP to have them written out.

I found this extremely useful, might be of help to you as well: https://www.computerhope.com/issues/ch001721.htm

Sign up to request clarification or add additional context in comments.

Comments

0

If by '"[<THIS " &" THAT THESE THOSE>]" are static' you mean that they are always the exact same string, then:

s = "[<THIS STRING-STRING-STRING THAT THESE THOSE>]"
before = len("[<THIS ")
after = len(" THAT THESE THOSE>]")
s[before:-after]
# 'STRING-STRING-STRING'

Comments

0

Like so (as long as the postition of the characters in the string doesn't change):

myString = "[<THIS STRING-STRING-STRING THAT THESE THOSE>]"
myString = myString[7:27]

2 Comments

You're assuming that the length of STRING-STRING-STRING is the same. Safer to assume that the parts you're NOT interested it are constant. String might be "Hello, my name is <NAME>. Isn't the weather nice today?" Length of <NAME> will vary.
I know, which is why I mentioned the character positions. But if OP was looking for a simple answer and it just so happened that they would be in the same position - here it is
0

Another alternative method;

import re
my_str = "[<THIS STRING-STRING-STRING THAT THESE THOSE>]"
string_pos = [(s.start(), s.end()) for s in list(re.finditer('STRING-STRING-STRING', my_str))]
start, end = string_pos[0]
print(my_str[start: end + 1])
STRING-STRING-STRING

If the STRING-STRING-STRING occurs multiple times in the string, start and end indexes of the each occurrences will be given as tuples in string_pos.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.