[英]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.
我想使用关键字在某个点压缩该字符串,以指示我在 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."
我想要从“纽约”到文本末尾的文本,即我需要代码来提取 substring“纽约市。我在华尔街工作。”
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.这样做是搜索您的 key_phrase,以及关键字在字符串中开始的索引 position。 Then it is using indexing to create the new string from where the key_phrase begins in the original string.
然后它使用索引从原始字符串中 key_phrase 开始的位置创建新字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.