如何在 python 中找到起点已知但终点未知的文本子串
How to find a substring of text with a known starting point but unknown ending point in python
我有一长串文字。我想在某个点压缩该字符串,使用一个关键字来指示 Python 中我的新字符串的开始。比如我的字符串是:
“你好,我叫约翰。我今年二十五岁。我住在纽约市。我在华尔街工作。”我想要从“纽约”到文本末尾的文本,即我需要代码来提取子字符串“纽约市。我在华尔街工作。”
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"
如有任何帮助,我们将不胜感激!
我认为最好的方法是使用正则表达式:
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.'
这是在搜索您的 key_phrase,以及关键字在字符串中开始的索引位置。然后它使用索引从原始字符串中 key_phrase 开始的位置创建新字符串。
我有一长串文字。我想在某个点压缩该字符串,使用一个关键字来指示 Python 中我的新字符串的开始。比如我的字符串是:
“你好,我叫约翰。我今年二十五岁。我住在纽约市。我在华尔街工作。”我想要从“纽约”到文本末尾的文本,即我需要代码来提取子字符串“纽约市。我在华尔街工作。”
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"
如有任何帮助,我们将不胜感激!
我认为最好的方法是使用正则表达式:
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.'
这是在搜索您的 key_phrase,以及关键字在字符串中开始的索引位置。然后它使用索引从原始字符串中 key_phrase 开始的位置创建新字符串。