简体   繁体   中英

How to extract a substring from a string in Python 3

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.

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'

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]

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM