简体   繁体   中英

How to find a substring of text with a known starting point but unknown ending point in python

I have a long string of text. I want to condense that string at a certain point using a key word to indicate the start of my new string in Python. For example, my string is:

"Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street." I want the text from "New York" to the end of the text ie I need code to pull the substring "New York City. I work on Wall Street."

have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"

Any help would be much appreciated!

I believe the best way to do this would be with regex:

import re

have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"

key_phrase_begins = re.search(key_phrase, have).span()[0]
new_string = have[key_phrase_begins:]
print(new_string) # Outputs: 'New York City. I work on Wall Street.'

What this is doing is searching for your key_phrase, and the index position at which the key phrase begins within the string. Then it is using indexing to create the new string from where the key_phrase begins in the original string.

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